Day 8
This commit is contained in:
1 parent
6b6484ffe8
commit
05821a58bd
4 files changed
+789
No files matched your search
Executable
+51
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys,re
|
||||
from math import lcm
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
lines = [line.strip('\n') for line in open(sys.argv[1])]
|
||||
lri = list(lines[0])
|
||||
network = {}
|
||||
start = []
|
||||
for line in lines[2:]:
|
||||
groups = re.search(r'(?P<start>.*) = \((?P<left>.*), (?P<right>.*)\)',line)
|
||||
network[groups['start']] = (groups['left'], groups['right'])
|
||||
if groups['start'][2] == 'A': start.append(groups['start'])
|
||||
|
||||
# challenge 1
|
||||
s, e = 'AAA', 'ZZZ'
|
||||
current = s
|
||||
instr = 0
|
||||
while current != e:
|
||||
i = 0 if lri[instr%len(lri)] == 'L' else 1
|
||||
current = network[current][i]
|
||||
instr += 1
|
||||
|
||||
res1 = str(instr)
|
||||
print(f"challenge 1:\n{res1}\n")
|
||||
|
||||
# challenge 2
|
||||
instr = [0] * len(start)
|
||||
ends = []
|
||||
for i in range(len(start)):
|
||||
ends.append([])
|
||||
for a,s in enumerate(start):
|
||||
current = s
|
||||
while True:
|
||||
i = 0 if lri[instr[a]%len(lri)] == 'L' else 1
|
||||
current = network[current][i]
|
||||
instr[a] += 1
|
||||
if current[2] == 'Z':
|
||||
b = False
|
||||
for c,u in ends[a]:
|
||||
if c == current and (instr[a] % u) == 0:
|
||||
b = True
|
||||
break
|
||||
if b: break
|
||||
ends[a].append((current, instr[a]))
|
||||
|
||||
res2 = str(lcm(*[e[0][1] for e in ends]))
|
||||
print(f"challenge 2:\n{res2}\n")
|
||||
|
||||
Reference in new issue
Block a user