2021-12-25 16:55:18 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import itertools
|
|
|
|
import sys
|
2024-11-23 20:24:55 +01:00
|
|
|
from typing import List, NamedTuple, Set
|
2021-12-25 16:55:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Point(NamedTuple):
|
|
|
|
x: int
|
|
|
|
y: int
|
|
|
|
|
|
|
|
|
|
|
|
class Map(NamedTuple):
|
|
|
|
east: Set[Point]
|
|
|
|
south: Set[Point]
|
|
|
|
dimensions: Point
|
|
|
|
|
|
|
|
|
|
|
|
def solve(input: List[str]) -> int:
|
|
|
|
def parse() -> Map:
|
|
|
|
east, south = set(), set()
|
|
|
|
|
|
|
|
for x, line in enumerate(input):
|
|
|
|
for y, c in enumerate(line):
|
|
|
|
if c == ".":
|
|
|
|
continue
|
|
|
|
if c == "v":
|
|
|
|
south.add(Point(x, y))
|
|
|
|
if c == ">":
|
|
|
|
east.add(Point(x, y))
|
|
|
|
|
|
|
|
return Map(east, south, Point(len(input), len(input[0])))
|
|
|
|
|
|
|
|
def step(sea_cucumbers: Map) -> Map:
|
|
|
|
def move_east(p: Point) -> Point:
|
|
|
|
return Point(p.x, (p.y + 1) % sea_cucumbers.dimensions.y)
|
|
|
|
|
|
|
|
def move_south(p: Point) -> Point:
|
|
|
|
return Point((p.x + 1) % sea_cucumbers.dimensions.x, p.y)
|
|
|
|
|
|
|
|
east, south = set(), set()
|
|
|
|
|
|
|
|
for old_p in sea_cucumbers.east:
|
|
|
|
p = move_east(old_p)
|
|
|
|
if p in sea_cucumbers.east or p in sea_cucumbers.south:
|
|
|
|
east.add(old_p)
|
|
|
|
continue
|
|
|
|
east.add(p)
|
|
|
|
|
|
|
|
for old_p in sea_cucumbers.south:
|
|
|
|
p = move_south(old_p)
|
|
|
|
if p in east or p in sea_cucumbers.south:
|
|
|
|
south.add(old_p)
|
|
|
|
continue
|
|
|
|
south.add(p)
|
|
|
|
|
|
|
|
return Map(east, south, sea_cucumbers.dimensions)
|
|
|
|
|
|
|
|
def debug(map: Map) -> None:
|
|
|
|
for x in range(map.dimensions.x):
|
|
|
|
print(
|
|
|
|
"".join(
|
2024-11-23 19:42:21 +01:00
|
|
|
(
|
|
|
|
"v"
|
|
|
|
if Point(x, y) in map.south
|
|
|
|
else ">" if Point(x, y) in map.east else "."
|
|
|
|
)
|
2021-12-25 16:55:18 +01:00
|
|
|
for y in range(map.dimensions.y)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
sea_cucumbers = parse()
|
|
|
|
for i in itertools.count(1):
|
|
|
|
if (new_map := step(sea_cucumbers)) == sea_cucumbers:
|
|
|
|
return i
|
|
|
|
print(i)
|
|
|
|
sea_cucumbers = new_map
|
|
|
|
|
|
|
|
assert False # Sanity check
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
input = [line.rstrip("\n") for line in sys.stdin.readlines()]
|
|
|
|
print(solve(input))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|