cleaned up day 8!
This commit is contained in:
parent
8a55c734e3
commit
8fcb93dcae
|
@ -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, ",")..")")
|
|
@ -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)
|
|
@ -0,0 +1,5 @@
|
|||
LLR
|
||||
|
||||
AAA = (BBB, BBB)
|
||||
BBB = (AAA, ZZZ)
|
||||
ZZZ = (ZZZ, ZZZ)
|
|
@ -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)
|
Loading…
Reference in New Issue