/ Python / 2021 / 25.py
25.py
 1  from lib import *
 2  
 3  input = read_input(2021, 25)
 4  
 5  lines = input.splitlines()
 6  
 7  
 8  right = set()
 9  down = set()
10  width = len(lines[0])
11  height = len(lines)
12  for i, line in enumerate(lines):
13      for j, c in enumerate(line):
14          match c:
15              case "v":
16                  down.add((j, i))
17              case ">":
18                  right.add((j, i))
19  
20  i = 0
21  while True:
22      old = {*right}, {*down}
23      right = {
24          new_pos if (new_pos := ((x + 1) % width, y)) not in right and new_pos not in down else (x, y) for x, y in right
25      }
26      down = {
27          new_pos if (new_pos := (x, (y + 1) % height)) not in right and new_pos not in down else (x, y) for x, y in down
28      }
29      i += 1
30      if (right, down) == old:
31          break
32  
33  print(i)