Compare commits

...

4 Commits

Author SHA1 Message Date
fbc2bc41e8 feat: solve day 11 puzzle 2 2025-12-11 12:59:29 +01:00
d0483124ac feat: solve day 11 puzzle 1 2025-12-11 12:45:52 +01:00
250be0f242 feat: solve day 10 puzzle 1 2025-12-10 15:10:53 +01:00
9f10ee83be feat: solve day 9 puzzle 1 2025-12-09 08:45:20 +01:00
12 changed files with 257 additions and 6 deletions

View File

@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
## Progress
<!-- calendar-start -->
#### Stars: 16/24
#### 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>|10<br>|11<br>|12<br>|||
|8<br>:star::star:|9<br>:star:|10<br>:star:|11<br>:star::star:|12<br>|||
<!-- calendar-end -->

8
res/examples/day09.txt Normal file
View 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
View 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
View 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
View 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

View File

@@ -32,16 +32,16 @@
"puzzle2": true
},
"day09": {
"puzzle1": false,
"puzzle1": true,
"puzzle2": false
},
"day10": {
"puzzle1": false,
"puzzle1": true,
"puzzle2": false
},
"day11": {
"puzzle1": false,
"puzzle2": false
"puzzle1": true,
"puzzle2": true
},
"day12": {
"puzzle1": false,

30
src/day09/puzzle1.lua Normal file
View 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
View File

@@ -0,0 +1,7 @@
local puzzle2 = {}
function puzzle2.solve(input)
return 0
end
return puzzle2

88
src/day10/puzzle1.lua Normal file
View 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
View File

@@ -0,0 +1,7 @@
local puzzle2 = {}
function puzzle2.solve(input)
return 0
end
return puzzle2

36
src/day11/puzzle1.lua Normal file
View 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
View 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