Compare commits
2 Commits
250be0f242
...
fbc2bc41e8
| Author | SHA1 | Date | |
|---|---|---|---|
|
fbc2bc41e8
|
|||
|
d0483124ac
|
@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
|
|||||||
## Progress
|
## Progress
|
||||||
|
|
||||||
<!-- calendar-start -->
|
<!-- calendar-start -->
|
||||||
#### Stars: 18/24
|
#### Stars: 20/24
|
||||||
|
|
||||||
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
|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:|
|
|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>|12<br>|||
|
|8<br>:star::star:|9<br>:star:|10<br>:star:|11<br>:star::star:|12<br>|||
|
||||||
<!-- calendar-end -->
|
<!-- calendar-end -->
|
||||||
|
|||||||
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
|
||||||
@@ -40,8 +40,8 @@
|
|||||||
"puzzle2": false
|
"puzzle2": false
|
||||||
},
|
},
|
||||||
"day11": {
|
"day11": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day12": {
|
"day12": {
|
||||||
"puzzle1": false,
|
"puzzle1": false,
|
||||||
|
|||||||
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
|
||||||
Reference in New Issue
Block a user