feat: solve day 10 puzzle 1
This commit is contained in:
@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
|
||||
## Progress
|
||||
|
||||
<!-- calendar-start -->
|
||||
#### Stars: 17/24
|
||||
#### Stars: 18/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>|11<br>|12<br>|||
|
||||
|8<br>:star::star:|9<br>:star:|10<br>:star:|11<br>|12<br>|||
|
||||
<!-- calendar-end -->
|
||||
|
||||
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}
|
||||
@@ -36,7 +36,7 @@
|
||||
"puzzle2": false
|
||||
},
|
||||
"day10": {
|
||||
"puzzle1": false,
|
||||
"puzzle1": true,
|
||||
"puzzle2": false
|
||||
},
|
||||
"day11": {
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user