Compare commits
31 Commits
c75a882e2c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
fbc2bc41e8
|
|||
|
d0483124ac
|
|||
|
250be0f242
|
|||
|
9f10ee83be
|
|||
|
c516532183
|
|||
|
3dedc1da03
|
|||
|
472634054f
|
|||
|
39c03d9920
|
|||
|
0add2ecd03
|
|||
|
d9ac0c7a9f
|
|||
|
bd36baa1ec
|
|||
|
80f797d4b9
|
|||
|
448dbf0ff9
|
|||
|
3fc4ac1700
|
|||
|
d0cc52fbda
|
|||
|
3be58692ab
|
|||
|
a08eaf0007
|
|||
|
fccf6464ba
|
|||
|
e1623ac0fa
|
|||
|
eb7dc5805f
|
|||
|
922aec35c1
|
|||
|
601e52d21e
|
|||
|
53d60f4a6d
|
|||
|
595c4e6fcc
|
|||
|
513d27c6c9
|
|||
|
b4445da204
|
|||
|
1378b7100a
|
|||
|
b059d8e381
|
|||
|
7490ea5b47
|
|||
|
d2752a04b6
|
|||
|
080f2b51c7
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
.vscode
|
||||||
|
res/inputs
|
||||||
17
README.md
Normal file
17
README.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Advent of Code
|
||||||
|
|
||||||
|
This repo contains my attempt at this year's Advent of Code (2025)
|
||||||
|
|
||||||
|
I will solve this AoC using Lua in the context of the ComputerCraft Minecraft mod.\
|
||||||
|
This project can also be run using the amazing [CraftOS-PC emulator](https://github.com/MCJack123/craftos2)
|
||||||
|
|
||||||
|
## Progress
|
||||||
|
|
||||||
|
<!-- calendar-start -->
|
||||||
|
#### Stars: 20/24
|
||||||
|
|
||||||
|
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
||||||
|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|
||||||
|
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star::star:|5<br>:star::star:|6<br>:star::star:|7<br>:star::star:|
|
||||||
|
|8<br>:star::star:|9<br>:star:|10<br>:star:|11<br>:star::star:|12<br>|||
|
||||||
|
<!-- calendar-end -->
|
||||||
10
res/examples/day01.txt
Normal file
10
res/examples/day01.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82
|
||||||
3
res/examples/day02.txt
Normal file
3
res/examples/day02.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
|
||||||
|
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
|
||||||
|
824824821-824824827,2121212118-2121212124
|
||||||
4
res/examples/day03.txt
Normal file
4
res/examples/day03.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111
|
||||||
10
res/examples/day04.txt
Normal file
10
res/examples/day04.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
..@@.@@@@.
|
||||||
|
@@@.@.@.@@
|
||||||
|
@@@@@.@.@@
|
||||||
|
@.@@@@..@.
|
||||||
|
@@.@@@@.@@
|
||||||
|
.@@@@@@@.@
|
||||||
|
.@.@.@.@@@
|
||||||
|
@.@@@.@@@@
|
||||||
|
.@@@@@@@@.
|
||||||
|
@.@.@@@.@.
|
||||||
11
res/examples/day05.txt
Normal file
11
res/examples/day05.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32
|
||||||
4
res/examples/day06.txt
Normal file
4
res/examples/day06.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
123 328 51 64
|
||||||
|
45 64 387 23
|
||||||
|
6 98 215 314
|
||||||
|
* + * +
|
||||||
16
res/examples/day07.txt
Normal file
16
res/examples/day07.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
.......S.......
|
||||||
|
...............
|
||||||
|
.......^.......
|
||||||
|
...............
|
||||||
|
......^.^......
|
||||||
|
...............
|
||||||
|
.....^.^.^.....
|
||||||
|
...............
|
||||||
|
....^.^...^....
|
||||||
|
...............
|
||||||
|
...^.^...^.^...
|
||||||
|
...............
|
||||||
|
..^...^.....^..
|
||||||
|
...............
|
||||||
|
.^.^.^.^.^...^.
|
||||||
|
...............
|
||||||
20
res/examples/day08.txt
Normal file
20
res/examples/day08.txt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
162,817,812
|
||||||
|
57,618,57
|
||||||
|
906,360,560
|
||||||
|
592,479,940
|
||||||
|
352,342,300
|
||||||
|
466,668,158
|
||||||
|
542,29,236
|
||||||
|
431,825,988
|
||||||
|
739,650,466
|
||||||
|
52,470,668
|
||||||
|
216,146,977
|
||||||
|
819,987,18
|
||||||
|
117,168,530
|
||||||
|
805,96,715
|
||||||
|
346,949,466
|
||||||
|
970,615,88
|
||||||
|
941,993,340
|
||||||
|
862,61,35
|
||||||
|
984,92,344
|
||||||
|
425,690,689
|
||||||
8
res/examples/day09.txt
Normal file
8
res/examples/day09.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
7,1
|
||||||
|
11,1
|
||||||
|
11,7
|
||||||
|
9,7
|
||||||
|
9,5
|
||||||
|
2,5
|
||||||
|
2,3
|
||||||
|
7,3
|
||||||
3
res/examples/day10.txt
Normal file
3
res/examples/day10.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}
|
||||||
|
[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}
|
||||||
|
[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}
|
||||||
13
res/examples/day11.txt
Normal file
13
res/examples/day11.txt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
svr: aaa bbb
|
||||||
|
aaa: fft
|
||||||
|
fft: ccc
|
||||||
|
bbb: tty
|
||||||
|
tty: ccc
|
||||||
|
ccc: ddd eee
|
||||||
|
ddd: hub
|
||||||
|
hub: fff
|
||||||
|
eee: dac
|
||||||
|
dac: fff
|
||||||
|
fff: ggg hhh
|
||||||
|
ggg: out
|
||||||
|
hhh: out
|
||||||
10
res/examples/day11_1.txt
Normal file
10
res/examples/day11_1.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
aaa: you hhh
|
||||||
|
you: bbb ccc
|
||||||
|
bbb: ddd eee
|
||||||
|
ccc: ddd eee fff
|
||||||
|
ddd: ggg
|
||||||
|
eee: out
|
||||||
|
fff: out
|
||||||
|
ggg: out
|
||||||
|
hhh: ccc fff iii
|
||||||
|
iii: out
|
||||||
@@ -1,51 +1,50 @@
|
|||||||
{
|
{
|
||||||
"day01": {
|
"day01": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day02": {
|
"day02": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day03": {
|
"day03": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day04": {
|
"day04": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day05": {
|
"day05": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day06": {
|
"day06": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day07": {
|
"day07": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day08": {
|
"day08": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day09": {
|
"day09": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": false
|
||||||
},
|
},
|
||||||
"day10": {
|
"day10": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": false
|
||||||
},
|
},
|
||||||
"day11": {
|
"day11": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day12": {
|
"day12": {
|
||||||
"puzzle1": false,
|
"puzzle1": false,
|
||||||
"puzzle2": false
|
"puzzle2": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
126
src/calendar.lua
Normal file
126
src/calendar.lua
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
package.path = "/aoc/src/lib/?.lua;" .. package.path
|
||||||
|
require("aoc")
|
||||||
|
local json = require("json")
|
||||||
|
local utils = require("utils")
|
||||||
|
local buffer = require("buffer")
|
||||||
|
local stats = json.loads(utils.readFile(RES_PATH .. "/stats.json")) or {}
|
||||||
|
local readmePath = ROOT_PATH .. "/README.md"
|
||||||
|
local outBuf = buffer.Buffer.new()
|
||||||
|
|
||||||
|
local function insertInReadme()
|
||||||
|
local body = utils.readFile(readmePath) or ""
|
||||||
|
local tagStart = "<!-- calendar-start -->"
|
||||||
|
local tagEnd = "<!-- calendar-end -->"
|
||||||
|
body = body:gsub(
|
||||||
|
tagStart:gsub("%-", "%%-") .. ".-" .. tagEnd:gsub("%-", "%%-"),
|
||||||
|
tagStart .. "\n" .. outBuf:tostring() .. tagEnd
|
||||||
|
)
|
||||||
|
utils.writeFile(readmePath, body, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
local totalStars = 0
|
||||||
|
---@diagnostic disable-next-line: param-type-mismatch
|
||||||
|
for _, s in pairs(stats) do
|
||||||
|
if s.puzzle1 then totalStars = totalStars + 1 end
|
||||||
|
if s.puzzle2 then totalStars = totalStars + 1 end
|
||||||
|
end
|
||||||
|
|
||||||
|
local startTS = 1764543600
|
||||||
|
|
||||||
|
local startDate = os.date("*t", startTS)
|
||||||
|
local firstWeekday = (startDate.wday + 5) % 7
|
||||||
|
local padding = {x=3, y=1}
|
||||||
|
|
||||||
|
local dayNames = {
|
||||||
|
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
|
||||||
|
}
|
||||||
|
|
||||||
|
local day = -firstWeekday + 1
|
||||||
|
|
||||||
|
local rowSep = "+"
|
||||||
|
local padRow = "|"
|
||||||
|
for _ = 1, 7 do
|
||||||
|
rowSep = rowSep .. string.rep("-", padding.x * 2 + 3) .. "+"
|
||||||
|
padRow = padRow .. string.rep(" ", padding.x * 2 + 3) .. "|"
|
||||||
|
end
|
||||||
|
|
||||||
|
local function printRow(cells, cellWidth, padding)
|
||||||
|
local xPad = string.rep(" ", padding.x)
|
||||||
|
local height = 0
|
||||||
|
for _, cell in ipairs(cells) do
|
||||||
|
height = math.max(height, type(cell) == "string" and 1 or #cell)
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = -padding.y + 1, height + padding.y do
|
||||||
|
write("|")
|
||||||
|
for _, cell in ipairs(cells) do
|
||||||
|
local text = cell[i] or ""
|
||||||
|
if type(cell) == "string" and i == 1 then
|
||||||
|
text = cell
|
||||||
|
end
|
||||||
|
local pad = cellWidth - #text
|
||||||
|
local lPad = math.ceil(pad / 2)
|
||||||
|
local rPad = pad - lPad
|
||||||
|
write(xPad .. string.rep(" ", lPad))
|
||||||
|
if i > 1 and i <= height then
|
||||||
|
term.setTextColor(colors.orange)
|
||||||
|
end
|
||||||
|
write(text)
|
||||||
|
term.setTextColor(colors.white)
|
||||||
|
write(string.rep(" ", rPad) .. xPad .. "|")
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
end
|
||||||
|
print(rowSep)
|
||||||
|
end
|
||||||
|
|
||||||
|
term.clear()
|
||||||
|
term.setCursorPos(1, 1)
|
||||||
|
term.setTextColor(colors.white)
|
||||||
|
|
||||||
|
print(("Stars: %d/24"):format(totalStars, 24))
|
||||||
|
outBuf:print(("#### Stars: %d/24\n"):format(totalStars, 24))
|
||||||
|
|
||||||
|
print(rowSep)
|
||||||
|
printRow(dayNames, 3, padding)
|
||||||
|
|
||||||
|
outBuf:print("|" .. table.concat(dayNames, "|") .. "|")
|
||||||
|
for _ = 1, 7 do
|
||||||
|
outBuf:write("|:-:")
|
||||||
|
end
|
||||||
|
outBuf:print("|")
|
||||||
|
|
||||||
|
while day < 12 do
|
||||||
|
local row = {}
|
||||||
|
local outRow = {}
|
||||||
|
for _ = 1, 7 do
|
||||||
|
if day < 1 or day > 12 then
|
||||||
|
table.insert(row, "")
|
||||||
|
table.insert(outRow, "")
|
||||||
|
else
|
||||||
|
local dayStats = stats[("day%02d"):format(day)]
|
||||||
|
local stars = 0
|
||||||
|
if dayStats.puzzle1 then stars = stars + 1 end
|
||||||
|
if dayStats.puzzle2 then stars = stars + 1 end
|
||||||
|
local cell = {
|
||||||
|
tostring(day),
|
||||||
|
string.rep("\x04", stars, " ")
|
||||||
|
}
|
||||||
|
table.insert(row, cell)
|
||||||
|
cell = {
|
||||||
|
tostring(day),
|
||||||
|
string.rep(":star:", stars, "")
|
||||||
|
}
|
||||||
|
table.insert(outRow, table.concat(cell, "<br>"))
|
||||||
|
end
|
||||||
|
day = day + 1
|
||||||
|
end
|
||||||
|
printRow(row, 3, padding)
|
||||||
|
outBuf:print("|" .. table.concat(outRow, "|") .. "|")
|
||||||
|
end
|
||||||
|
|
||||||
|
insertInReadme()
|
||||||
|
|
||||||
|
---@diagnostic disable-next-line: undefined-field
|
||||||
|
term.screenshot()
|
||||||
|
os.sleep(2)
|
||||||
25
src/day01/puzzle1.lua
Normal file
25
src/day01/puzzle1.lua
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local password = 0
|
||||||
|
local cursor = 50
|
||||||
|
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
local dir = line:sub(1, 1)
|
||||||
|
local dist = tonumber(line:sub(2))
|
||||||
|
if dir == "R" then
|
||||||
|
cursor = cursor + dist
|
||||||
|
else
|
||||||
|
cursor = cursor - dist
|
||||||
|
end
|
||||||
|
cursor = cursor % 100
|
||||||
|
if cursor == 0 then
|
||||||
|
password = password + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return password
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
31
src/day01/puzzle2.lua
Normal file
31
src/day01/puzzle2.lua
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local password = 0
|
||||||
|
local cursor = 50
|
||||||
|
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
local dir = line:sub(1, 1)
|
||||||
|
local dist = tonumber(line:sub(2))
|
||||||
|
if dir == "R" then
|
||||||
|
cursor = cursor + dist
|
||||||
|
if cursor > 99 then
|
||||||
|
password = password + math.floor(cursor / 100)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
cursor = cursor - dist
|
||||||
|
if cursor <= 0 then
|
||||||
|
if cursor ~= -dist then
|
||||||
|
password = password + 1
|
||||||
|
end
|
||||||
|
password = password + math.floor(-cursor / 100)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
cursor = cursor % 100
|
||||||
|
end
|
||||||
|
return password
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
38
src/day02/puzzle1.lua
Normal file
38
src/day02/puzzle1.lua
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.splitRange(range)
|
||||||
|
local parts = utils.split(range, "-")
|
||||||
|
return tonumber(parts[1]), tonumber(parts[2])
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.isValid(id)
|
||||||
|
local str = tostring(id)
|
||||||
|
if #str % 2 == 1 then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
local mid = #str / 2
|
||||||
|
return str:sub(1, mid) ~= str:sub(mid + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.countInvalids(range)
|
||||||
|
local min, max = puzzle1.splitRange(range)
|
||||||
|
local total = 0
|
||||||
|
for i=min, max do
|
||||||
|
if not puzzle1.isValid(i) then
|
||||||
|
total = total + i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local ranges = utils.split(input, ",")
|
||||||
|
local total = 0
|
||||||
|
for _, range in ipairs(ranges) do
|
||||||
|
total = total + puzzle1.countInvalids(range)
|
||||||
|
end
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
46
src/day02/puzzle2.lua
Normal file
46
src/day02/puzzle2.lua
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = require(SRC_PATH .. "/day02/puzzle1")
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.isValid(id)
|
||||||
|
local str = tostring(id)
|
||||||
|
local totLen = #str
|
||||||
|
for len=1, totLen - 1 do
|
||||||
|
if totLen % len == 0 then
|
||||||
|
local ref = str:sub(1, len)
|
||||||
|
local allSame = true
|
||||||
|
for i=1, totLen / len - 1 do
|
||||||
|
if str:sub(len * i + 1, len * (i + 1)) ~= ref then
|
||||||
|
allSame = false
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if allSame then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.countInvalids(range)
|
||||||
|
local min, max = puzzle1.splitRange(range)
|
||||||
|
local total = 0
|
||||||
|
for i=min, max do
|
||||||
|
if not puzzle2.isValid(i) then
|
||||||
|
total = total + i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local ranges = utils.split(input, ",")
|
||||||
|
local total = 0
|
||||||
|
for _, range in ipairs(ranges) do
|
||||||
|
total = total + puzzle2.countInvalids(range)
|
||||||
|
end
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
41
src/day03/puzzle1.lua
Normal file
41
src/day03/puzzle1.lua
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
local utils = require("utils")
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.bankJoltages(bank)
|
||||||
|
local joltages = {}
|
||||||
|
for i=1, #bank do
|
||||||
|
table.insert(joltages, tonumber(bank:sub(i, i)))
|
||||||
|
end
|
||||||
|
return joltages
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.findMaxJoltage(bank)
|
||||||
|
local maxTens = 0
|
||||||
|
local maxJoltage = 0
|
||||||
|
local joltages = puzzle1.bankJoltages(bank)
|
||||||
|
|
||||||
|
for i, tens in ipairs(joltages) do
|
||||||
|
if tens > maxTens then
|
||||||
|
for j=i+1, #joltages do
|
||||||
|
local ones = joltages[j]
|
||||||
|
local joltage = tens * 10 + ones
|
||||||
|
if joltage > maxJoltage then
|
||||||
|
maxTens = tens
|
||||||
|
maxJoltage = joltage
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return maxJoltage
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local banks = utils.splitLines(input)
|
||||||
|
local totalJoltage = 0
|
||||||
|
for _, bank in ipairs(banks) do
|
||||||
|
totalJoltage = totalJoltage + puzzle1.findMaxJoltage(bank)
|
||||||
|
end
|
||||||
|
return totalJoltage
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
64
src/day03/puzzle2.lua
Normal file
64
src/day03/puzzle2.lua
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
local utils = require("utils")
|
||||||
|
local puzzle1 = require(SRC_PATH .. "/day03/puzzle1")
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
local cache = {}
|
||||||
|
|
||||||
|
function puzzle2.findMaxJoltage(bank, joltages, n, startI)
|
||||||
|
startI = startI or 1
|
||||||
|
local key = bank:sub(startI) .. "-" .. tostring(n)
|
||||||
|
if cache[key] then
|
||||||
|
return cache[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
local maxJoltage = 0
|
||||||
|
local maxDigit = 0
|
||||||
|
|
||||||
|
if startI > #joltages then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if n == 1 then
|
||||||
|
for i=startI, #joltages do
|
||||||
|
if joltages[i] > maxDigit then
|
||||||
|
maxDigit = joltages[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
cache[key] = maxDigit
|
||||||
|
return maxDigit
|
||||||
|
end
|
||||||
|
|
||||||
|
local i = startI
|
||||||
|
while i <= #joltages do
|
||||||
|
local digit = joltages[i]
|
||||||
|
if digit > maxDigit then
|
||||||
|
local maxSuffix = puzzle2.findMaxJoltage(bank, joltages, n - 1, i + 1)
|
||||||
|
if maxSuffix ~= nil then
|
||||||
|
local joltage = tonumber(tostring(digit) .. tostring(maxSuffix))
|
||||||
|
if joltage > maxJoltage then
|
||||||
|
maxDigit = digit
|
||||||
|
maxJoltage = joltage
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
i = i + 1
|
||||||
|
end
|
||||||
|
if maxJoltage == 0 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
cache[key] = maxJoltage
|
||||||
|
return maxJoltage
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local banks = utils.splitLines(input)
|
||||||
|
local totalJoltage = 0
|
||||||
|
for _, bank in ipairs(banks) do
|
||||||
|
local joltages = puzzle1.bankJoltages(bank)
|
||||||
|
local max = puzzle2.findMaxJoltage(bank, joltages, 12)
|
||||||
|
totalJoltage = totalJoltage + max
|
||||||
|
end
|
||||||
|
return totalJoltage
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
44
src/day04/puzzle1.lua
Normal file
44
src/day04/puzzle1.lua
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.inGrid(x, y, w, h)
|
||||||
|
return 1 <= x and x <= w and 1 <= y and y <= h
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.isAccessible(lines, w, h, x, y)
|
||||||
|
local neighbors = 0
|
||||||
|
for dy=-1, 1 do
|
||||||
|
for dx=-1, 1 do
|
||||||
|
if dx ~=0 or dy ~= 0 then
|
||||||
|
local x2 = x + dx
|
||||||
|
local y2 = y + dy
|
||||||
|
if puzzle1.inGrid(x2, y2, w, h) then
|
||||||
|
if lines[y2]:sub(x2, x2) == "@" then
|
||||||
|
neighbors = neighbors + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return neighbors < 4
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
local accessible = 0
|
||||||
|
|
||||||
|
local h = #lines
|
||||||
|
local w = #lines[1]
|
||||||
|
|
||||||
|
for y, line in ipairs(lines) do
|
||||||
|
for x=1, #line do
|
||||||
|
if line:sub(x, x) == "@" and puzzle1.isAccessible(lines, w, h, x, y) then
|
||||||
|
accessible = accessible + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return accessible
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
106
src/day04/puzzle2.lua
Normal file
106
src/day04/puzzle2.lua
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.inGrid(x, y, w, h)
|
||||||
|
return 1 <= x and x <= w and 1 <= y and y <= h
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.countNeighbors(lines, w, h, x, y)
|
||||||
|
local neighbors = 0
|
||||||
|
for dy=-1, 1 do
|
||||||
|
for dx=-1, 1 do
|
||||||
|
if dx ~=0 or dy ~= 0 then
|
||||||
|
local x2 = x + dx
|
||||||
|
local y2 = y + dy
|
||||||
|
if puzzle2.inGrid(x2, y2, w, h) then
|
||||||
|
if lines[y2]:sub(x2, x2) == "@" then
|
||||||
|
neighbors = neighbors + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return neighbors
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.copyGrid(grid)
|
||||||
|
local grid2 = {}
|
||||||
|
for y, row in ipairs(grid) do
|
||||||
|
local row2 = {}
|
||||||
|
for x, n in ipairs(row) do
|
||||||
|
row2[x] = n
|
||||||
|
end
|
||||||
|
grid2[y] = row2
|
||||||
|
end
|
||||||
|
return grid2
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.removeRoll(grid, x, y, w, h)
|
||||||
|
for dy=-1, 1 do
|
||||||
|
for dx=-1, 1 do
|
||||||
|
if dx ~= 0 or dy ~= 0 then
|
||||||
|
local x2 = x + dx
|
||||||
|
local y2 = y + dy
|
||||||
|
if puzzle2.inGrid(x2, y2, w, h) then
|
||||||
|
if grid[y2][x2] > 0 then
|
||||||
|
grid[y2][x2] = grid[y2][x2] - 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
grid[y][x] = -1
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.removeRolls(grid, w, h)
|
||||||
|
local removed = 0
|
||||||
|
local grid2 = puzzle2.copyGrid(grid)
|
||||||
|
|
||||||
|
for y, row in ipairs(grid) do
|
||||||
|
for x, n in ipairs(row) do
|
||||||
|
if 0 <= n and n < 4 then
|
||||||
|
puzzle2.removeRoll(grid2, x, y, w, h)
|
||||||
|
removed = removed + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return removed, grid2
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
|
||||||
|
local h = #lines
|
||||||
|
local w = #lines[1]
|
||||||
|
|
||||||
|
local grid = {}
|
||||||
|
|
||||||
|
for y, line in ipairs(lines) do
|
||||||
|
local row = {}
|
||||||
|
for x=1, #line do
|
||||||
|
local neighbors = -1
|
||||||
|
if line:sub(x, x) == "@" then
|
||||||
|
neighbors = puzzle2.countNeighbors(lines, w, h, x, y)
|
||||||
|
end
|
||||||
|
row[x] = neighbors
|
||||||
|
end
|
||||||
|
grid[y] = row
|
||||||
|
end
|
||||||
|
|
||||||
|
local totalRemoved = 0
|
||||||
|
|
||||||
|
local removed = 0
|
||||||
|
while true do
|
||||||
|
removed, grid = puzzle2.removeRolls(grid, w, h)
|
||||||
|
if removed == 0 then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
totalRemoved = totalRemoved + removed
|
||||||
|
end
|
||||||
|
|
||||||
|
return totalRemoved
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
39
src/day05/puzzle1.lua
Normal file
39
src/day05/puzzle1.lua
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
local strings = require "cc.strings"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.isFresh(ranges, i)
|
||||||
|
for _, range in ipairs(ranges) do
|
||||||
|
if range[1] <= i and i <= range[2] then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local lines = strings.split(input, "\n")
|
||||||
|
local ranges = {}
|
||||||
|
|
||||||
|
local bIngr = false
|
||||||
|
local totalFresh = 0
|
||||||
|
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
if line == "" then
|
||||||
|
bIngr = true
|
||||||
|
else
|
||||||
|
if bIngr then
|
||||||
|
local ingr = tonumber(line)
|
||||||
|
if puzzle1.isFresh(ranges, ingr) then
|
||||||
|
totalFresh = totalFresh + 1
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local min, max = line:match("(%d+)%-(%d+)")
|
||||||
|
table.insert(ranges, {tonumber(min), tonumber(max)})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return totalFresh
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
47
src/day05/puzzle2.lua
Normal file
47
src/day05/puzzle2.lua
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
local strings = require "cc.strings"
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local lines = strings.split(input, "\n")
|
||||||
|
local bounds = {}
|
||||||
|
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
if line == "" then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
local min, max = line:match("(%d+)%-(%d+)")
|
||||||
|
min = tonumber(min)
|
||||||
|
max = tonumber(max)
|
||||||
|
table.insert(bounds, {i=min, type="start"})
|
||||||
|
table.insert(bounds, {i=max, type="end"})
|
||||||
|
end
|
||||||
|
|
||||||
|
table.sort(bounds, function (a, b)
|
||||||
|
if a.i == b.i then
|
||||||
|
return a.type == "start" and b.type == "end"
|
||||||
|
end
|
||||||
|
return a.i < b.i
|
||||||
|
end)
|
||||||
|
|
||||||
|
local totalFresh = 0
|
||||||
|
local min = 0
|
||||||
|
local balance = 0
|
||||||
|
|
||||||
|
for _, bound in ipairs(bounds) do
|
||||||
|
if bound.type == "start" then
|
||||||
|
if balance == 0 then
|
||||||
|
min = bound.i
|
||||||
|
end
|
||||||
|
balance = balance + 1
|
||||||
|
else
|
||||||
|
balance = balance - 1
|
||||||
|
if balance == 0 then
|
||||||
|
totalFresh = totalFresh + bound.i - min + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return totalFresh
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
39
src/day06/puzzle1.lua
Normal file
39
src/day06/puzzle1.lua
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
local strings = require "cc.strings"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local lines = strings.split(input, "\n")
|
||||||
|
local problems = {}
|
||||||
|
local n = #lines
|
||||||
|
|
||||||
|
local ops = strings.split(lines[n], "%s+")
|
||||||
|
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
local j = 1
|
||||||
|
for num in line:gmatch("(%d+)") do
|
||||||
|
local op = ops[j]
|
||||||
|
if i == 1 then
|
||||||
|
if op == "+" then
|
||||||
|
table.insert(problems, 0)
|
||||||
|
else
|
||||||
|
table.insert(problems, 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local val = tonumber(num)
|
||||||
|
if op == "+" then
|
||||||
|
problems[j] = problems[j] + val
|
||||||
|
else
|
||||||
|
problems[j] = problems[j] * val
|
||||||
|
end
|
||||||
|
j = j + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local total = 0
|
||||||
|
for _, val in ipairs(problems) do
|
||||||
|
total = total + val
|
||||||
|
end
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
58
src/day06/puzzle2.lua
Normal file
58
src/day06/puzzle2.lua
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
local strings = require "cc.strings"
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.parseNumbers(lines)
|
||||||
|
local numbers = {}
|
||||||
|
local n = #lines
|
||||||
|
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
if i ~= n then
|
||||||
|
for j=1, #line do
|
||||||
|
local c = line:sub(j, j)
|
||||||
|
if #numbers < j then
|
||||||
|
table.insert(numbers, {})
|
||||||
|
end
|
||||||
|
if c ~= " " then
|
||||||
|
table.insert(numbers[j], c)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local problems = {{}}
|
||||||
|
for _, num in ipairs(numbers) do
|
||||||
|
if #num == 0 then
|
||||||
|
table.insert(problems, {})
|
||||||
|
else
|
||||||
|
local value = tonumber(table.concat(num, ""))
|
||||||
|
table.insert(problems[#problems], value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return problems
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local lines = strings.split(input, "\n")
|
||||||
|
local values = puzzle2.parseNumbers(lines)
|
||||||
|
local ops = strings.split(lines[#lines], "%s+")
|
||||||
|
|
||||||
|
local total = 0
|
||||||
|
for p, numbers in ipairs(values) do
|
||||||
|
local op = ops[p]
|
||||||
|
local value = 0
|
||||||
|
if op == "*" then
|
||||||
|
value = 1
|
||||||
|
end
|
||||||
|
for _, num in ipairs(numbers) do
|
||||||
|
if op == "+" then
|
||||||
|
value = value + num
|
||||||
|
else
|
||||||
|
value = value * num
|
||||||
|
end
|
||||||
|
end
|
||||||
|
total = total + value
|
||||||
|
end
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
34
src/day07/puzzle1.lua
Normal file
34
src/day07/puzzle1.lua
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
|
||||||
|
local beams = {}
|
||||||
|
local totalSplits = 0
|
||||||
|
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
local newBeams = {}
|
||||||
|
if i == 1 then
|
||||||
|
newBeams[line:find("S")] = true
|
||||||
|
else
|
||||||
|
for j=1, #line do
|
||||||
|
if beams[j] then
|
||||||
|
if line:sub(j, j) == "^" then
|
||||||
|
totalSplits = totalSplits + 1
|
||||||
|
newBeams[j - 1] = true
|
||||||
|
newBeams[j + 1] = true
|
||||||
|
else
|
||||||
|
newBeams[j] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
beams = newBeams
|
||||||
|
end
|
||||||
|
|
||||||
|
return totalSplits
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
37
src/day07/puzzle2.lua
Normal file
37
src/day07/puzzle2.lua
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
|
||||||
|
local beams = {}
|
||||||
|
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
local newBeams = {}
|
||||||
|
if i == 1 then
|
||||||
|
newBeams[line:find("S")] = 1
|
||||||
|
else
|
||||||
|
for j=1, #line do
|
||||||
|
if beams[j] then
|
||||||
|
if line:sub(j, j) == "^" then
|
||||||
|
newBeams[j - 1] = (newBeams[j - 1] or 0) + beams[j]
|
||||||
|
newBeams[j + 1] = (newBeams[j + 1] or 0) + beams[j]
|
||||||
|
else
|
||||||
|
newBeams[j] = (newBeams[j] or 0) + beams[j]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
beams = newBeams
|
||||||
|
end
|
||||||
|
|
||||||
|
local finalBeams = 0
|
||||||
|
for _, v in pairs(beams) do
|
||||||
|
finalBeams = finalBeams + v
|
||||||
|
end
|
||||||
|
|
||||||
|
return finalBeams
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
82
src/day08/puzzle1.lua
Normal file
82
src/day08/puzzle1.lua
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.dist(box1, box2)
|
||||||
|
return math.sqrt(
|
||||||
|
(box2.x - box1.x) ^ 2 +
|
||||||
|
(box2.y - box1.y) ^ 2 +
|
||||||
|
(box2.z - box1.z) ^ 2
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.pretty(box)
|
||||||
|
return ("%d,%d,%d"):format(box.x, box.y, box.z)
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local boxes = {}
|
||||||
|
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
local x, y, z = line:match("(%d+),(%d+),(%d+)")
|
||||||
|
table.insert(boxes, {x=x, y=y, z=z, i=i, circuit=i})
|
||||||
|
end
|
||||||
|
|
||||||
|
local dists = {}
|
||||||
|
local circuits = {}
|
||||||
|
|
||||||
|
for i, box1 in ipairs(boxes) do
|
||||||
|
circuits[i] = i
|
||||||
|
for j=i+1, #boxes do
|
||||||
|
local box2 = boxes[j]
|
||||||
|
table.insert(dists, {
|
||||||
|
i=i,
|
||||||
|
j=j,
|
||||||
|
dist=puzzle1.dist(box1, box2)
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
table.sort(dists, function (a, b)
|
||||||
|
return a.dist < b.dist
|
||||||
|
end)
|
||||||
|
|
||||||
|
local N = 1000
|
||||||
|
if #boxes < 100 then
|
||||||
|
N = 10
|
||||||
|
end
|
||||||
|
|
||||||
|
for i, d in ipairs(dists) do
|
||||||
|
if i > N then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
local circ1 = circuits[d.i]
|
||||||
|
local circ2 = circuits[d.j]
|
||||||
|
|
||||||
|
for k, v in pairs(circuits) do
|
||||||
|
if v == circ2 then
|
||||||
|
circuits[k] = circ1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local circuitSizes = {}
|
||||||
|
|
||||||
|
for _, c in pairs(circuits) do
|
||||||
|
circuitSizes[c] = (circuitSizes[c] or 0) + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local sizes = {}
|
||||||
|
|
||||||
|
for _, s in pairs(circuitSizes) do
|
||||||
|
table.insert(sizes, s)
|
||||||
|
end
|
||||||
|
|
||||||
|
table.sort(sizes)
|
||||||
|
local n = #sizes
|
||||||
|
|
||||||
|
return sizes[n] * sizes[n - 1] * sizes[n - 2]
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
52
src/day08/puzzle2.lua
Normal file
52
src/day08/puzzle2.lua
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = require(SRC_PATH .. "/day08/puzzle1")
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local boxes = {}
|
||||||
|
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
local x, y, z = line:match("(%d+),(%d+),(%d+)")
|
||||||
|
table.insert(boxes, {x=x, y=y, z=z, i=i, circuit=i})
|
||||||
|
end
|
||||||
|
|
||||||
|
local dists = {}
|
||||||
|
local circuits = {}
|
||||||
|
|
||||||
|
for i, box1 in ipairs(boxes) do
|
||||||
|
circuits[i] = i
|
||||||
|
for j=i+1, #boxes do
|
||||||
|
local box2 = boxes[j]
|
||||||
|
table.insert(dists, {
|
||||||
|
i=i,
|
||||||
|
j=j,
|
||||||
|
dist=puzzle1.dist(box1, box2)
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
table.sort(dists, function (a, b)
|
||||||
|
return a.dist < b.dist
|
||||||
|
end)
|
||||||
|
|
||||||
|
for _, d in ipairs(dists) do
|
||||||
|
local circ1 = circuits[d.i]
|
||||||
|
local circ2 = circuits[d.j]
|
||||||
|
|
||||||
|
local connex = true
|
||||||
|
for k, v in pairs(circuits) do
|
||||||
|
if v == circ2 then
|
||||||
|
circuits[k] = circ1
|
||||||
|
elseif v ~= circ1 then
|
||||||
|
connex = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if connex then
|
||||||
|
return boxes[d.i].x * boxes[d.j].x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
30
src/day09/puzzle1.lua
Normal file
30
src/day09/puzzle1.lua
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.area(tile1, tile2)
|
||||||
|
local dx = math.abs(tile2.x - tile1.x) + 1
|
||||||
|
local dy = math.abs(tile2.y - tile1.y) + 1
|
||||||
|
return dx * dy
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
local tiles = {}
|
||||||
|
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
local x, y = line:match("(%d+),(%d+)")
|
||||||
|
table.insert(tiles, {x=tonumber(x), y=tonumber(y)})
|
||||||
|
end
|
||||||
|
|
||||||
|
local maxArea = 0
|
||||||
|
for i, tile1 in ipairs(tiles) do
|
||||||
|
for j=i+1, #tiles do
|
||||||
|
local tile2 = tiles[j]
|
||||||
|
local area = puzzle1.area(tile1, tile2)
|
||||||
|
maxArea = math.max(maxArea, area)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return maxArea
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
7
src/day09/puzzle2.lua
Normal file
7
src/day09/puzzle2.lua
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
88
src/day10/puzzle1.lua
Normal file
88
src/day10/puzzle1.lua
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
local strings = require "cc.strings"
|
||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
function puzzle1.parseMachine(line)
|
||||||
|
local lightsStr = line:match("%[([.#]+)%]")
|
||||||
|
local buttonsStr = strings.split(line:match(" (.+) "), " ")
|
||||||
|
|
||||||
|
local lightsBools = {}
|
||||||
|
local lightsVal = 0
|
||||||
|
for i=1, #lightsStr do
|
||||||
|
local c = lightsStr:sub(i, i)
|
||||||
|
lightsBools[i] = c == "#"
|
||||||
|
if c == "#" then
|
||||||
|
local pow = bit.blshift(1, i - 1)
|
||||||
|
lightsVal = lightsVal + pow
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local buttons = {}
|
||||||
|
|
||||||
|
for _, btnStr in ipairs(buttonsStr) do
|
||||||
|
local lights = {}
|
||||||
|
local value = 0
|
||||||
|
for n in btnStr:gmatch("%d+") do
|
||||||
|
local v = tonumber(n)
|
||||||
|
table.insert(lights, v)
|
||||||
|
value = value + bit.blshift(1, v)
|
||||||
|
end
|
||||||
|
table.insert(buttons, {
|
||||||
|
lights=lights,
|
||||||
|
value=value,
|
||||||
|
presses=0
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
lightsBools=lightsBools,
|
||||||
|
lightsVal=lightsVal,
|
||||||
|
buttons=buttons
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.startMachine(target, btns)
|
||||||
|
local queue = {{value=0, presses=0, used=0}}
|
||||||
|
local cache = {}
|
||||||
|
while #queue ~= 0 do
|
||||||
|
local test = table.remove(queue, 1)
|
||||||
|
local key = tostring(test.value) .. "-" .. tostring(test.used)
|
||||||
|
if not cache[key] then
|
||||||
|
cache[key] = true
|
||||||
|
if test.value == target then
|
||||||
|
return test.presses
|
||||||
|
end
|
||||||
|
for i=(test.presses + 1), #btns do
|
||||||
|
local pow = bit.blshift(1, i - 1)
|
||||||
|
if bit.band(test.used, pow) == 0 then
|
||||||
|
table.insert(queue, {
|
||||||
|
value=bit.bxor(test.value, btns[i].value),
|
||||||
|
presses=test.presses + 1,
|
||||||
|
used=test.used + pow
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print("Could not find valid button combination")
|
||||||
|
return -1
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
local machines = {}
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
table.insert(machines, puzzle1.parseMachine(line))
|
||||||
|
end
|
||||||
|
|
||||||
|
local totalPresses = 0
|
||||||
|
|
||||||
|
for i, machine in ipairs(machines) do
|
||||||
|
print("Machine " .. tostring(i) .. "/" .. tostring(#machines))
|
||||||
|
totalPresses = totalPresses + puzzle1.startMachine(machine.lightsVal, machine.buttons)
|
||||||
|
end
|
||||||
|
|
||||||
|
return totalPresses
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
7
src/day10/puzzle2.lua
Normal file
7
src/day10/puzzle2.lua
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
36
src/day11/puzzle1.lua
Normal file
36
src/day11/puzzle1.lua
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
local strings = require "cc.strings"
|
||||||
|
local utils = require "utils"
|
||||||
|
local puzzle1 = {}
|
||||||
|
|
||||||
|
local cache = {}
|
||||||
|
|
||||||
|
function puzzle1.countPaths(edges, startDev, endDev)
|
||||||
|
local key = startDev .. "-" .. endDev
|
||||||
|
if cache[key] then
|
||||||
|
return cache[key]
|
||||||
|
end
|
||||||
|
if startDev == endDev then
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
local outs = edges[startDev]
|
||||||
|
local total = 0
|
||||||
|
for _, out in ipairs(outs) do
|
||||||
|
total = total + puzzle1.countPaths(edges, out, endDev)
|
||||||
|
end
|
||||||
|
cache[key] = total
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle1.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
local outputs = {}
|
||||||
|
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
local device, outs = line:match("(.+): (.+)")
|
||||||
|
outputs[device] = strings.split(outs, " ")
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1.countPaths(outputs, "you", "out")
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle1
|
||||||
49
src/day11/puzzle2.lua
Normal file
49
src/day11/puzzle2.lua
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
local strings = require "cc.strings"
|
||||||
|
local utils = require "utils"
|
||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
local cache = {}
|
||||||
|
|
||||||
|
function puzzle2.countPaths(edges, startDev, endDev, dac, fft)
|
||||||
|
dac = dac or false
|
||||||
|
fft = fft or false
|
||||||
|
local key = startDev .. "-" .. endDev .. "-" .. tostring(dac) .. "-" .. tostring(fft)
|
||||||
|
if cache[key] then
|
||||||
|
return cache[key]
|
||||||
|
end
|
||||||
|
if startDev == endDev then
|
||||||
|
if dac and fft then
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local outs = edges[startDev]
|
||||||
|
local total = 0
|
||||||
|
for _, out in ipairs(outs) do
|
||||||
|
local dac2 = dac
|
||||||
|
local fft2 = fft
|
||||||
|
if out == "dac" then
|
||||||
|
dac2 = true
|
||||||
|
elseif out == "fft" then
|
||||||
|
fft2 = true
|
||||||
|
end
|
||||||
|
total = total + puzzle2.countPaths(edges, out, endDev, dac2, fft2)
|
||||||
|
end
|
||||||
|
cache[key] = total
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
local lines = utils.splitLines(input)
|
||||||
|
local outputs = {}
|
||||||
|
|
||||||
|
for _, line in ipairs(lines) do
|
||||||
|
local device, outs = line:match("(.+): (.+)")
|
||||||
|
outputs[device] = strings.split(outs, " ")
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2.countPaths(outputs, "svr", "out")
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
SRC_PATH = "/aoc/src"
|
ROOT_PATH = "/aoc"
|
||||||
RES_PATH = "/aoc/res"
|
SRC_PATH = ROOT_PATH .. "/src"
|
||||||
|
RES_PATH = ROOT_PATH .. "/res"
|
||||||
|
CACHE_PATH = "/.cache/aoc"
|
||||||
|
|
||||||
package.path = SRC_PATH .. "/lib/?.lua;" .. package.path
|
package.path = SRC_PATH .. "/lib/?.lua;" .. package.path
|
||||||
|
|
||||||
@@ -9,25 +11,17 @@ END_DATE = {day=12, month=12, year=2025}
|
|||||||
local json = require("json")
|
local json = require("json")
|
||||||
local dates = require("dates")
|
local dates = require("dates")
|
||||||
local days = require("days")
|
local days = require("days")
|
||||||
|
local utils = require("utils")
|
||||||
|
local progress = require "progress"
|
||||||
local today = os.date("*t")
|
local today = os.date("*t")
|
||||||
|
local aoc = {}
|
||||||
|
|
||||||
local function loadStats(path)
|
function aoc.loadStats()
|
||||||
path = shell.resolve(path)
|
local data = utils.readFile(RES_PATH .. "/stats.json") or "{}"
|
||||||
if not fs.exists(path) then
|
return json.loads(data)
|
||||||
printError("Stats file not found (" .. path .. ")")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local file, err = fs.open(path, "r")
|
|
||||||
if not file then
|
|
||||||
printError("Cannot open stats file")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local data = json.loads(file.readAll())
|
|
||||||
file.close()
|
|
||||||
return data
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function printDateInfo()
|
function aoc.printDateInfo()
|
||||||
if dates.isBefore(START_DATE, today) then
|
if dates.isBefore(START_DATE, today) then
|
||||||
print("AoC 2025 has not started yet")
|
print("AoC 2025 has not started yet")
|
||||||
return
|
return
|
||||||
@@ -40,7 +34,7 @@ local function printDateInfo()
|
|||||||
print("Day " .. day .. "/" .. END_DATE.day)
|
print("Day " .. day .. "/" .. END_DATE.day)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function printStats(stats, selected)
|
function aoc.printStats(stats, selected)
|
||||||
local keys = {}
|
local keys = {}
|
||||||
for k in pairs(stats) do
|
for k in pairs(stats) do
|
||||||
table.insert(keys, k)
|
table.insert(keys, k)
|
||||||
@@ -55,43 +49,50 @@ local function printStats(stats, selected)
|
|||||||
local date = {day=day, month=START_DATE.month, year=START_DATE.year}
|
local date = {day=day, month=START_DATE.month, year=START_DATE.year}
|
||||||
term.setTextColor(colors.lightBlue)
|
term.setTextColor(colors.lightBlue)
|
||||||
if selected == day then
|
if selected == day then
|
||||||
|
term.setBackgroundColor(colors.gray)
|
||||||
write("- ")
|
write("- ")
|
||||||
else
|
else
|
||||||
|
term.setBackgroundColor(colors.black)
|
||||||
write(" ")
|
write(" ")
|
||||||
end
|
end
|
||||||
if not dates.isBefore(date, today) then
|
if not dates.isBefore(date, today) then
|
||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
else
|
else
|
||||||
term.setTextColor(colors.gray)
|
term.setTextColor(colors.lightGray)
|
||||||
end
|
end
|
||||||
write(string.format("Day %2s ", day))
|
write(string.format("Day %2s ", day))
|
||||||
if value.puzzle1 then
|
if value.puzzle1 then
|
||||||
term.setTextColor(colors.orange)
|
term.setTextColor(colors.orange)
|
||||||
stars = stars + 1
|
stars = stars + 1
|
||||||
else
|
else
|
||||||
term.setTextColor(colors.gray)
|
term.setTextColor(colors.lightGray)
|
||||||
end
|
end
|
||||||
write("\x04")
|
write("\x04")
|
||||||
if value.puzzle2 then
|
if value.puzzle2 then
|
||||||
term.setTextColor(colors.orange)
|
term.setTextColor(colors.orange)
|
||||||
stars = stars + 1
|
stars = stars + 1
|
||||||
else
|
else
|
||||||
term.setTextColor(colors.gray)
|
term.setTextColor(colors.lightGray)
|
||||||
end
|
end
|
||||||
write("\x04")
|
write("\x04")
|
||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
print()
|
print(" ")
|
||||||
end
|
end
|
||||||
|
term.setBackgroundColor(colors.black)
|
||||||
|
|
||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
write(string.format("You have %d", stars))
|
write(string.format("You have %d", stars))
|
||||||
term.setTextColor(colors.orange)
|
term.setTextColor(colors.orange)
|
||||||
write("\x04")
|
write("\x04 ")
|
||||||
|
local x, y = term.getCursorPos()
|
||||||
|
progress.bar(x, y, 20, stars, 24, colors.orange, colors.gray)
|
||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
print()
|
print()
|
||||||
|
print()
|
||||||
|
print("Press END to quit")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function printBanner()
|
function aoc.printBanner()
|
||||||
term.setTextColor(colors.green)
|
term.setTextColor(colors.green)
|
||||||
print("+--------------------------------------+")
|
print("+--------------------------------------+")
|
||||||
print("| Welcome to the Advent of Code 2025 |")
|
print("| Welcome to the Advent of Code 2025 |")
|
||||||
@@ -99,8 +100,8 @@ local function printBanner()
|
|||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function main()
|
function aoc.main()
|
||||||
local stats = loadStats("aoc/res/stats.json")
|
local stats = aoc.loadStats() or {}
|
||||||
local selectedDay = math.max(1, math.min(END_DATE.day, today.day))
|
local selectedDay = math.max(1, math.min(END_DATE.day, today.day))
|
||||||
if not dates.isInDateRange(START_DATE, today, END_DATE) then
|
if not dates.isInDateRange(START_DATE, today, END_DATE) then
|
||||||
selectedDay = 1
|
selectedDay = 1
|
||||||
@@ -109,9 +110,9 @@ local function main()
|
|||||||
while true do
|
while true do
|
||||||
term.clear()
|
term.clear()
|
||||||
term.setCursorPos(1, 1)
|
term.setCursorPos(1, 1)
|
||||||
printBanner()
|
aoc.printBanner()
|
||||||
printDateInfo()
|
aoc.printDateInfo()
|
||||||
printStats(stats, selectedDay)
|
aoc.printStats(stats, selectedDay)
|
||||||
|
|
||||||
local event, key, is_held = os.pullEvent("key")
|
local event, key, is_held = os.pullEvent("key")
|
||||||
if key == keys.up then
|
if key == keys.up then
|
||||||
@@ -128,8 +129,9 @@ local function main()
|
|||||||
dayStats.puzzle2
|
dayStats.puzzle2
|
||||||
)
|
)
|
||||||
day:show()
|
day:show()
|
||||||
|
stats = aoc.loadStats() or {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
main()
|
return aoc
|
||||||
|
|||||||
35
src/lib/buffer.lua
Normal file
35
src/lib/buffer.lua
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
local buffer = {}
|
||||||
|
|
||||||
|
---@class Buffer
|
||||||
|
local Buffer = {_buf=""}
|
||||||
|
Buffer.__index = Buffer
|
||||||
|
|
||||||
|
---Creates a new buffer
|
||||||
|
---@param str string?
|
||||||
|
---@return Buffer
|
||||||
|
function Buffer.new(str)
|
||||||
|
local buf = {}
|
||||||
|
buf._buf = str or ""
|
||||||
|
setmetatable(buf, Buffer)
|
||||||
|
return buf
|
||||||
|
end
|
||||||
|
|
||||||
|
function Buffer:tostring()
|
||||||
|
return self._buf
|
||||||
|
end
|
||||||
|
|
||||||
|
function Buffer:clear()
|
||||||
|
self._buf = ""
|
||||||
|
end
|
||||||
|
|
||||||
|
function Buffer:write(text)
|
||||||
|
self._buf = self._buf .. (text or "")
|
||||||
|
end
|
||||||
|
|
||||||
|
function Buffer:print(text)
|
||||||
|
self:write((text or "") .. "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
buffer.Buffer = Buffer
|
||||||
|
|
||||||
|
return buffer
|
||||||
163
src/lib/days.lua
163
src/lib/days.lua
@@ -1,14 +1,8 @@
|
|||||||
|
local json = require("json")
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
local days = {}
|
local days = {}
|
||||||
|
|
||||||
local DAY_CACHE_PATH = "/.cache/days"
|
local DAY_CACHE_PATH = CACHE_PATH .. "/days"
|
||||||
|
|
||||||
local CHOICES = {
|
|
||||||
create = "Create files",
|
|
||||||
example = "Run examples",
|
|
||||||
real = "Run with real input",
|
|
||||||
main = "Back to main menu"
|
|
||||||
}
|
|
||||||
|
|
||||||
local PUZZLE_BASE = [[local puzzle%d = {}
|
local PUZZLE_BASE = [[local puzzle%d = {}
|
||||||
|
|
||||||
@@ -24,7 +18,8 @@ return puzzle%d
|
|||||||
---@field title string?
|
---@field title string?
|
||||||
---@field puzzle1 boolean
|
---@field puzzle1 boolean
|
||||||
---@field puzzle2 boolean
|
---@field puzzle2 boolean
|
||||||
local Day = {day = 0, title = nil, puzzle1 = false, puzzle2 = false}
|
---@field results table
|
||||||
|
local Day = {day = 0, title = nil, puzzle1 = false, puzzle2 = false, results={}}
|
||||||
Day.__index = Day
|
Day.__index = Day
|
||||||
|
|
||||||
---Creates a new Day object
|
---Creates a new Day object
|
||||||
@@ -38,9 +33,14 @@ function Day.new(dayI, puzzle1, puzzle2)
|
|||||||
day.day = dayI
|
day.day = dayI
|
||||||
day.puzzle1 = puzzle1
|
day.puzzle1 = puzzle1
|
||||||
day.puzzle2 = puzzle2
|
day.puzzle2 = puzzle2
|
||||||
|
day:loadResults()
|
||||||
return day
|
return day
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Day:cacheDir()
|
||||||
|
return DAY_CACHE_PATH .. ("/%02d"):format(self.day)
|
||||||
|
end
|
||||||
|
|
||||||
---Returns the title of this day.
|
---Returns the title of this day.
|
||||||
---
|
---
|
||||||
---This function looks in the following places, in order:
|
---This function looks in the following places, in order:
|
||||||
@@ -52,7 +52,7 @@ function Day:getTitle()
|
|||||||
if self.title then
|
if self.title then
|
||||||
return self.title
|
return self.title
|
||||||
end
|
end
|
||||||
local cachePath = DAY_CACHE_PATH .. ("/%02d.txt"):format(self.day)
|
local cachePath = self:cacheDir() .. "/name.txt"
|
||||||
if fs.exists(cachePath) then
|
if fs.exists(cachePath) then
|
||||||
local cache = fs.open(cachePath, "r")
|
local cache = fs.open(cachePath, "r")
|
||||||
if cache then
|
if cache then
|
||||||
@@ -64,8 +64,8 @@ function Day:getTitle()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
fs.makeDir(DAY_CACHE_PATH)
|
fs.makeDir(self:cacheDir())
|
||||||
local res = http.get("https://adventofcode.com/2024/day/" .. self.day)
|
local res = http.get("https://adventofcode.com/2025/day/" .. self.day)
|
||||||
local title = "Day " .. self.day
|
local title = "Day " .. self.day
|
||||||
if res then
|
if res then
|
||||||
local body = res.readAll() or ""
|
local body = res.readAll() or ""
|
||||||
@@ -125,31 +125,119 @@ function Day:getInputData()
|
|||||||
return utils.readFile(self:inputPath())
|
return utils.readFile(self:inputPath())
|
||||||
end
|
end
|
||||||
|
|
||||||
function Day:execPuzzle(puzzleI, data)
|
function Day:getResultKey(real, puzzle, suffix)
|
||||||
local puzzle = require(self:srcDir() .. "/puzzle" .. puzzleI)
|
local key = ""
|
||||||
|
if real then
|
||||||
|
key = "input"
|
||||||
|
else
|
||||||
|
key = "example"
|
||||||
|
if suffix then
|
||||||
|
key = key .. "_" .. suffix
|
||||||
|
end
|
||||||
|
end
|
||||||
|
key = key .. "-" .. puzzle
|
||||||
|
return key
|
||||||
|
end
|
||||||
|
|
||||||
|
function Day:execPuzzle(puzzleI, data, resultKey)
|
||||||
|
local path = self:srcDir() .. "/puzzle" .. puzzleI
|
||||||
|
package.loaded[path] = nil
|
||||||
|
local puzzle = require(path)
|
||||||
local t0 = os.epoch("local")
|
local t0 = os.epoch("local")
|
||||||
local result = puzzle.solve(data)
|
local result = puzzle.solve(data)
|
||||||
local t1 = os.epoch("local")
|
local t1 = os.epoch("local")
|
||||||
print(("(Executed in %.3fs)"):format((t1 - t0) / 1000))
|
print(("(Executed in %.3fs)"):format((t1 - t0) / 1000))
|
||||||
print("Result:", result)
|
print(("Result: %.0f"):format(result))
|
||||||
|
self.results[resultKey] = result
|
||||||
|
self:saveResults()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Day:execExample(puzzleI, suffix)
|
function Day:execExample(puzzleI, suffix)
|
||||||
local data = self:getExampleData(suffix)
|
local data = self:getExampleData(suffix)
|
||||||
return self:execPuzzle(puzzleI, data)
|
return self:execPuzzle(puzzleI, data, self:getResultKey(false, puzzleI, suffix))
|
||||||
end
|
end
|
||||||
|
|
||||||
function Day:execReal(puzzleI)
|
function Day:execReal(puzzleI)
|
||||||
local data = self:getInputData()
|
local data = self:getInputData()
|
||||||
return self:execPuzzle(puzzleI, data)
|
return self:execPuzzle(puzzleI, data, self:getResultKey(true, puzzleI))
|
||||||
end
|
end
|
||||||
|
|
||||||
function Day:choosePuzzle()
|
function Day:choosePuzzle(real)
|
||||||
self:printTitle()
|
self:printTitle()
|
||||||
local c = utils.promptChoices({"Puzzle 1", "Puzzle 2", "Back"})
|
local choices = {
|
||||||
|
{"Puzzle 1", 1},
|
||||||
|
{"Puzzle 2", 2},
|
||||||
|
"Back"
|
||||||
|
}
|
||||||
|
local res1 = self.results[self:getResultKey(real, 1)]
|
||||||
|
local res2 = self.results[self:getResultKey(real, 2)]
|
||||||
|
if res1 then choices[1][1] = choices[1][1] .. (" - %s"):format(res1) end
|
||||||
|
if res2 then choices[2][1] = choices[2][1] .. (" - %s"):format(res2) end
|
||||||
|
local c = utils.promptChoices(choices)
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Day:menuStars()
|
||||||
|
local solvedTxt = function (b)
|
||||||
|
if b then
|
||||||
|
return "solved"
|
||||||
|
end
|
||||||
|
return "unsolved"
|
||||||
|
end
|
||||||
|
local c0 = 1
|
||||||
|
while true do
|
||||||
|
self:printTitle()
|
||||||
|
local choices = {
|
||||||
|
{"Mark puzzle 1 as " .. solvedTxt(not self.puzzle1), 1},
|
||||||
|
{"Mark puzzle 2 as " .. solvedTxt(not self.puzzle2), 2},
|
||||||
|
"Back"
|
||||||
|
}
|
||||||
|
local c = utils.promptChoices(choices, c0)
|
||||||
|
if c == "Back" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
c0 = c
|
||||||
|
if c == 1 then
|
||||||
|
self.puzzle1 = not self.puzzle1
|
||||||
|
elseif c == 2 then
|
||||||
|
self.puzzle2 = not self.puzzle2
|
||||||
|
end
|
||||||
|
self:saveStats()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Day:saveStats()
|
||||||
|
local path = RES_PATH .. "/stats.json"
|
||||||
|
local content = utils.readFile(path) or "{}"
|
||||||
|
local data = json.loads(content) or {}
|
||||||
|
data[("day%02d"):format(self.day)] = {
|
||||||
|
puzzle1=self.puzzle1,
|
||||||
|
puzzle2=self.puzzle2,
|
||||||
|
}
|
||||||
|
content = json.dumps(data, 4, true)
|
||||||
|
utils.writeFile(path, content, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Day:loadResults()
|
||||||
|
local path = self:cacheDir() .. "/results.json"
|
||||||
|
local results = {}
|
||||||
|
if fs.exists(path) then
|
||||||
|
local data = utils.readFile(path)
|
||||||
|
local r = json.loads(data)
|
||||||
|
if type(r) == "table" then
|
||||||
|
results = r
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self.results = results
|
||||||
|
end
|
||||||
|
|
||||||
|
function Day:saveResults()
|
||||||
|
local data = json.dumps(self.results)
|
||||||
|
fs.makeDir(self:cacheDir())
|
||||||
|
local path = self:cacheDir() .. "/results.json"
|
||||||
|
utils.writeFile(path, data, true)
|
||||||
|
end
|
||||||
|
|
||||||
function Day:printTitle()
|
function Day:printTitle()
|
||||||
term.clear()
|
term.clear()
|
||||||
term.setCursorPos(1, 1)
|
term.setCursorPos(1, 1)
|
||||||
@@ -163,28 +251,35 @@ function Day:show()
|
|||||||
while true do
|
while true do
|
||||||
self:printTitle()
|
self:printTitle()
|
||||||
if fs.exists(self:srcDir()) then
|
if fs.exists(self:srcDir()) then
|
||||||
local c = utils.promptChoices({CHOICES.example, CHOICES.real, CHOICES.main})
|
local c = utils.promptChoices({
|
||||||
if c == CHOICES.main then
|
{"Run examples", "examples"},
|
||||||
|
{"Run with real input", "inputs"},
|
||||||
|
{"Mark solved", "stars"},
|
||||||
|
{"Back to main menu", "main"}
|
||||||
|
})
|
||||||
|
if c == "main" then
|
||||||
return
|
return
|
||||||
end
|
elseif c == "stars" then
|
||||||
local puzzle = self:choosePuzzle()
|
self:menuStars()
|
||||||
|
else
|
||||||
|
local puzzle = self:choosePuzzle(c == "inputs")
|
||||||
if puzzle ~= "Back" then
|
if puzzle ~= "Back" then
|
||||||
local puzzleI = ({
|
if c == "examples" then
|
||||||
["Puzzle 1"] = 1,
|
self:execExample(puzzle)
|
||||||
["Puzzle 2"] = 2
|
elseif c == "inputs" then
|
||||||
})[puzzle]
|
self:execReal(puzzle)
|
||||||
if c == CHOICES.example then
|
|
||||||
self:execExample(puzzleI)
|
|
||||||
elseif c == CHOICES.real then
|
|
||||||
self:execReal(puzzleI)
|
|
||||||
end
|
end
|
||||||
utils.waitForKey(keys.enter)
|
utils.waitForKey(keys.enter)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
local c = utils.promptChoices({CHOICES.create, CHOICES.main})
|
local c = utils.promptChoices({
|
||||||
if c == CHOICES.main then
|
{"Create files", "create"},
|
||||||
|
{"Back to main menu", "main"}
|
||||||
|
})
|
||||||
|
if c == "main" then
|
||||||
return
|
return
|
||||||
elseif c == CHOICES.create then
|
elseif c == "create" then
|
||||||
self:createFiles()
|
self:createFiles()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,6 +20,13 @@ local function errFactory(prefix)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function ifNil(test, ifNil, ifNotNil)
|
||||||
|
if test == nil then
|
||||||
|
return ifNil
|
||||||
|
end
|
||||||
|
return ifNotNil
|
||||||
|
end
|
||||||
|
|
||||||
local function parseObj(data)
|
local function parseObj(data)
|
||||||
local printErr = errFactory("Error while parsing object")
|
local printErr = errFactory("Error while parsing object")
|
||||||
local obj = {}
|
local obj = {}
|
||||||
@@ -199,4 +206,86 @@ function json.loads(data)
|
|||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function kind(obj)
|
||||||
|
if type(obj) ~= "table" then
|
||||||
|
return type(obj)
|
||||||
|
end
|
||||||
|
local i = 1
|
||||||
|
for _ in pairs(obj) do
|
||||||
|
if obj[i] ~= nil then
|
||||||
|
i = i + 1
|
||||||
|
else
|
||||||
|
return "table"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if i == 1 then
|
||||||
|
return "table"
|
||||||
|
end
|
||||||
|
return "array"
|
||||||
|
end
|
||||||
|
|
||||||
|
local function escapeStr(str)
|
||||||
|
local in_char = {'\\', '"', '/', '\b', '\f', '\n', '\r', '\t'}
|
||||||
|
local out_char = {'\\', '"', '/', 'b', 'f', 'n', 'r', 't'}
|
||||||
|
for i, c in ipairs(in_char) do
|
||||||
|
str = str:gsub(c, '\\' .. out_char[i])
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
function json.dumps(data, indent, sortKeys, depth)
|
||||||
|
if data == json.null then
|
||||||
|
return "null"
|
||||||
|
end
|
||||||
|
|
||||||
|
local t = kind(data)
|
||||||
|
if t == "string" then
|
||||||
|
return '"' .. escapeStr(data) .. '"'
|
||||||
|
elseif t == "number" or t == "boolean" then
|
||||||
|
return tostring(data)
|
||||||
|
end
|
||||||
|
|
||||||
|
depth = depth or 0
|
||||||
|
local spaces = ""
|
||||||
|
if indent ~= nil then
|
||||||
|
spaces = string.rep(" ", indent * depth)
|
||||||
|
end
|
||||||
|
local res = ""
|
||||||
|
|
||||||
|
if t == "array" then
|
||||||
|
res = res .. "[" .. ifNil(indent, "", "\n")
|
||||||
|
for i, val in ipairs(data) do
|
||||||
|
if i > 1 then
|
||||||
|
res = res .. "," .. ifNil(indent, " ", "\n")
|
||||||
|
end
|
||||||
|
res = res .. spaces .. string.rep(" ", indent or 0)
|
||||||
|
res = res .. json.dumps(val, indent, sortKeys, depth + 1)
|
||||||
|
end
|
||||||
|
res = res .. ifNil(indent, "", "\n") .. spaces .. "]"
|
||||||
|
elseif t == "table" then
|
||||||
|
res = res .. "{" .. ifNil(indent, "", "\n")
|
||||||
|
local first = true
|
||||||
|
local keys = {}
|
||||||
|
for k, _ in pairs(data) do
|
||||||
|
table.insert(keys, k)
|
||||||
|
end
|
||||||
|
if sortKeys then
|
||||||
|
table.sort(keys)
|
||||||
|
end
|
||||||
|
for _, k in ipairs(keys) do
|
||||||
|
local v = data[k]
|
||||||
|
if not first then
|
||||||
|
res = res .. "," .. ifNil(indent, " ", "\n")
|
||||||
|
end
|
||||||
|
first = false
|
||||||
|
res = res .. spaces .. string.rep(" ", indent or 0)
|
||||||
|
res = res .. '"' .. escapeStr(k) .. '": '
|
||||||
|
res = res .. json.dumps(v, indent, sortKeys, depth + 1)
|
||||||
|
end
|
||||||
|
res = res .. ifNil(indent, "", "\n") .. spaces .. "}"
|
||||||
|
end
|
||||||
|
|
||||||
|
return res
|
||||||
|
end
|
||||||
|
|
||||||
return json
|
return json
|
||||||
14
src/lib/progress.lua
Normal file
14
src/lib/progress.lua
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
local utils = require("utils")
|
||||||
|
local progress = {}
|
||||||
|
|
||||||
|
function progress.bar(x, y, width, value, max, fg, bg)
|
||||||
|
local fgWidth = utils.round(value * width / max)
|
||||||
|
fgWidth = math.max(0, math.min(width, fgWidth))
|
||||||
|
paintutils.drawLine(x, y, x + width - 1, y, bg)
|
||||||
|
if fgWidth > 0 then
|
||||||
|
paintutils.drawLine(x, y, x + fgWidth - 1, y, fg)
|
||||||
|
end
|
||||||
|
term.setBackgroundColor(colors.black)
|
||||||
|
end
|
||||||
|
|
||||||
|
return progress
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
local utils = {}
|
local utils = {}
|
||||||
|
|
||||||
function utils.promptChoices(choices)
|
function utils.promptChoices(choices, default)
|
||||||
local c = 1
|
local c = default or 1
|
||||||
local ox, oy = term.getCursorPos()
|
local ox, oy = term.getCursorPos()
|
||||||
while true do
|
while true do
|
||||||
term.setCursorPos(ox, oy)
|
term.setCursorPos(ox, oy)
|
||||||
@@ -11,7 +11,11 @@ function utils.promptChoices(choices)
|
|||||||
else
|
else
|
||||||
term.setTextColor(colors.lightGray)
|
term.setTextColor(colors.lightGray)
|
||||||
end
|
end
|
||||||
print(choice)
|
local label = choice
|
||||||
|
if type(choice) == "table" then
|
||||||
|
label = choice[1] or choice["label"]
|
||||||
|
end
|
||||||
|
print(label)
|
||||||
end
|
end
|
||||||
local event, key, is_held = os.pullEvent("key")
|
local event, key, is_held = os.pullEvent("key")
|
||||||
if key == keys.up then
|
if key == keys.up then
|
||||||
@@ -23,7 +27,11 @@ function utils.promptChoices(choices)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
return choices[c]
|
local choice = choices[c]
|
||||||
|
if type(choice) == "table" then
|
||||||
|
choice = choice[2] or choice["value"]
|
||||||
|
end
|
||||||
|
return choice
|
||||||
end
|
end
|
||||||
|
|
||||||
function utils.readFile(path)
|
function utils.readFile(path)
|
||||||
@@ -70,4 +78,20 @@ function utils.waitForKey(targetKey)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function utils.split(data, sep)
|
||||||
|
local t = {}
|
||||||
|
for str in string.gmatch(data, "([^" .. sep .. "]+)") do
|
||||||
|
table.insert(t, str)
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
function utils.splitLines(data)
|
||||||
|
return utils.split(data, "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
function utils.round(x)
|
||||||
|
return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
|
||||||
|
end
|
||||||
|
|
||||||
return utils
|
return utils
|
||||||
|
|||||||
Reference in New Issue
Block a user