From 250be0f242068084c817cdae66506f3936eb7e29 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 10 Dec 2025 15:10:53 +0100 Subject: [PATCH] feat: solve day 10 puzzle 1 --- README.md | 4 +- res/examples/day10.txt | 3 ++ res/stats.json | 2 +- src/day10/puzzle1.lua | 88 ++++++++++++++++++++++++++++++++++++++++++ src/day10/puzzle2.lua | 7 ++++ 5 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 res/examples/day10.txt create mode 100644 src/day10/puzzle1.lua create mode 100644 src/day10/puzzle2.lua diff --git a/README.md b/README.md index 70878d6..8e39b0b 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git ## Progress -#### Stars: 17/24 +#### Stars: 18/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| |1
:star::star:|2
:star::star:|3
:star::star:|4
:star::star:|5
:star::star:|6
:star::star:|7
:star::star:| -|8
:star::star:|9
:star:|10
|11
|12
||| +|8
:star::star:|9
:star:|10
:star:|11
|12
||| diff --git a/res/examples/day10.txt b/res/examples/day10.txt new file mode 100644 index 0000000..3a9b1fe --- /dev/null +++ b/res/examples/day10.txt @@ -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} \ No newline at end of file diff --git a/res/stats.json b/res/stats.json index 6919d68..95f10e1 100644 --- a/res/stats.json +++ b/res/stats.json @@ -36,7 +36,7 @@ "puzzle2": false }, "day10": { - "puzzle1": false, + "puzzle1": true, "puzzle2": false }, "day11": { diff --git a/src/day10/puzzle1.lua b/src/day10/puzzle1.lua new file mode 100644 index 0000000..022aa7f --- /dev/null +++ b/src/day10/puzzle1.lua @@ -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 diff --git a/src/day10/puzzle2.lua b/src/day10/puzzle2.lua new file mode 100644 index 0000000..2f4bc9a --- /dev/null +++ b/src/day10/puzzle2.lua @@ -0,0 +1,7 @@ +local puzzle2 = {} + +function puzzle2.solve(input) + return 0 +end + +return puzzle2