advent2023/eight.py

20 lines
506 B
Python
Executable File

#!/usr/bin/env python3
from useful import *
(header, ), footer = hfl(open(0))
kart = {m[0]: (m[1], m[2]) for m in (re.findall(r'\w+', line) for line in footer)}
def time(start, stop):
guide = map(lambda x: 0 if x == 'L' else 1, cycle(header))
steps = 0
now = start
while not re.match(stop, now):
now = kart[now][next(guide)]
steps += 1
return steps
print(time('AAA', 'ZZZ'))
print(math.lcm(*[time(start, '..Z') for start in kart.keys() if re.match('..A', start)]))