Skip to content

Commit f52e52c

Browse files
committed
Day 25
1 parent fe1c0bb commit f52e52c

File tree

7 files changed

+260
-0
lines changed

7 files changed

+260
-0
lines changed

day_25/__init__.py

Whitespace-only changes.

day_25/__main__.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from typing import TextIO, Dict, List, Set
2+
import sys
3+
from collections import defaultdict
4+
import enum
5+
from dataclasses import dataclass
6+
7+
8+
9+
class CucumberDirection(enum.Enum):
10+
DOWN = 'v',
11+
RIGHT = '>'
12+
13+
@dataclass(frozen=True, eq=True)
14+
class Point:
15+
x: int
16+
y: int
17+
18+
19+
class World:
20+
@staticmethod
21+
def read_input(textio: TextIO):
22+
max_x = max_y = 0
23+
seafloor = dict()
24+
cukes = defaultdict(lambda: set())
25+
for y, line in enumerate(textio.readlines()):
26+
max_y = max(max_y, y)
27+
for x, c in enumerate(line.strip()):
28+
max_x = max(max_x, x)
29+
loc = Point(x,y)
30+
match c:
31+
case 'v':
32+
cuke_dir = CucumberDirection.DOWN
33+
case '>':
34+
cuke_dir = CucumberDirection.RIGHT
35+
case _:
36+
continue
37+
seafloor[loc] = cuke_dir
38+
cukes[cuke_dir].add(loc)
39+
return World(seafloor, Point(max_x+1, max_y+1), cukes)
40+
41+
def __init__(self, seafloor: Dict[Point, CucumberDirection], size: Point, cukes: Dict[CucumberDirection, Set[Point]]):
42+
self.seafloor = seafloor
43+
self.size = size
44+
self.cukes = cukes
45+
46+
def __str__(self):
47+
out = ''
48+
for y in range(self.size.y):
49+
for x in range(self.size.x):
50+
loc = Point(x,y)
51+
if loc in self.seafloor:
52+
out += self.seafloor[loc].value[0]
53+
else:
54+
out += '.'
55+
out += '\n'
56+
return out[:-1]
57+
58+
def _next_loc(self, cuke: Point, direction: CucumberDirection):
59+
match direction:
60+
case CucumberDirection.DOWN:
61+
x = cuke.x
62+
y = (cuke.y + 1) % self.size.y
63+
64+
case CucumberDirection.RIGHT:
65+
x = (cuke.x + 1) % self.size.x
66+
y = cuke.y
67+
68+
return Point(x,y)
69+
70+
def step(self):
71+
something_moved = False
72+
for direction in [CucumberDirection.RIGHT, CucumberDirection.DOWN]:
73+
movers = []
74+
for cuke in self.cukes[direction]:
75+
next_loc = self._next_loc(cuke, direction)
76+
if next_loc not in self.seafloor:
77+
movers.append((cuke, next_loc))
78+
79+
for from_loc, to_loc in movers:
80+
something_moved = True
81+
del self.seafloor[from_loc]
82+
self.seafloor[to_loc] = direction
83+
self.cukes[direction].remove(from_loc)
84+
self.cukes[direction].add(to_loc)
85+
print(len(movers))
86+
return something_moved
87+
88+
89+
world = World.read_input(sys.stdin)
90+
step = 0
91+
while True:
92+
# print("\033c", end="")
93+
# print(world)
94+
print(step)
95+
# print()
96+
something_moved = world.step()
97+
step += 1
98+
99+
if not something_moved:
100+
print(f'Stopped moving after {step} steps')
101+
break
102+

day_25/input

Lines changed: 137 additions & 0 deletions
Large diffs are not rendered by default.

day_25/test1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
...>>>>>...

day_25/test2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
..........
2+
.>v....v..
3+
.......>..
4+
..........

day_25/test3

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
...>...
2+
.......
3+
......>
4+
v.....>
5+
......>
6+
.......
7+
..vvv..

day_25/test4

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
v...>>.vv>
2+
.vv>>.vv..
3+
>>.>v>...v
4+
>>v>>.>.v.
5+
v>v.vv.v..
6+
>.>>..v...
7+
.vv..>.>v.
8+
v.v..>>v.v
9+
....v..v.>

0 commit comments

Comments
 (0)