cleaned up day 8!

This commit is contained in:
sejo 2023-12-08 07:08:36 +01:00
parent 8a55c734e3
commit 8fcb93dcae
4 changed files with 93 additions and 0 deletions

69
12023/08/08.lua Normal file
View File

@ -0,0 +1,69 @@
io.input("input")
ins = io.read()
_ = io.read()
map = {}
countA = 0
countZ = 0
endA = {}
for line in io.lines() do
node, left, right = line:match("(%a+)[%= %(]+(%a+), (%a+)")
map[node] = {L=left, R=right}
if node:sub(3,3)=="A" then
countA=countA+1
table.insert(endA, node)
end
end
--part 1
i = 1
count = 1
curr = "AAA"
repeat
step = ins:sub(i,i)
sig = map[curr][step]
--print(curr, step, ">",sig)
if sig~="ZZZ" then
i = i==#ins and 1 or i+1
count = count + 1
curr = sig
end
until sig=="ZZZ"
print("part 1", count)
--
--part 2
i = 1
count = 1
curr = {}
periods = {}
table.move(endA, 1, #endA, 1, curr)
repeat
step = ins:sub(i,i)
sig = {}
finZ = true
for j, c in ipairs(curr) do
s = map[c][step]
sig[j] = s
if s:sub(3,3)~="Z" then
finZ = false
else
--print(c, step,">", s, count)
table.insert(periods,count)
sig[j] = nil
end
end
if not finZ then
i = i==#ins and 1 or i+1
count = count + 1
curr = {}
for j,n in pairs(sig) do
table.insert(curr, n)
end
end
until finZ
print("part 2: lcm("..table.concat(periods, ",")..")")

9
12023/08/test Normal file
View File

@ -0,0 +1,9 @@
RL
AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)

5
12023/08/test2 Normal file
View File

@ -0,0 +1,5 @@
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)

10
12023/08/test3 Normal file
View File

@ -0,0 +1,10 @@
LR
aaA = (aaB, XXX)
aaB = (XXX, aaZ)
aaZ = (aaB, XXX)
bbA = (bbB, XXX)
bbB = (bbC, bbC)
bbC = (bbZ, bbZ)
bbZ = (bbB, bbB)
XXX = (XXX, XXX)