Advertisement
hhoppe

Advent of code 2023 day 8 concise

Dec 8th, 2023 (edited)
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. def day8(s, *, part2=False):
  2.   lines = s.splitlines()
  3.   routes = {line[:3]: (line[7:10], line[12:15]) for line in lines[2:]}
  4.  
  5.   def length(node, end):
  6.     for index, ch in enumerate(itertools.cycle(lines[0])):
  7.       if node.endswith(end):
  8.         return index
  9.       node = routes[node][ch == 'R']
  10.  
  11.   if part2:
  12.     return math.lcm(*(length(node, 'Z') for node in routes if node.endswith('A')))
  13.   return length('AAA', 'ZZZ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement