-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.py
50 lines (41 loc) · 1.42 KB
/
day25.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
from dataclasses import dataclass
@dataclass
class Cucumber:
x: int
y: int
horizontal: bool
def cucumberLocations(cucumbers):
return { (c.x, c.y): c.horizontal for c in cucumbers }
def moveCucumbers(cucumbers, horizontal, xmax, ymax):
moves = 0
locationsBeforeMove = cucumberLocations(cucumbers)
for cucumber in cucumbers:
if (horizontal ^ cucumber.horizontal):
continue
dx, dy = (1,0) if horizontal else (0,1)
x, y = cucumber.x, cucumber.y
next = ((x+dx) % (xmax+1), (y+dy) % (ymax+1))
if next not in locationsBeforeMove:
moves += 1
cucumber.x, cucumber.y = next
return moves
def moving(cucumbers, xmax, ymax):
return moveCucumbers(cucumbers, True, xmax, ymax) + moveCucumbers(cucumbers, False, xmax, ymax)
def main(args = ()):
fileName = "day25.txt" if len(args) < 1 else args[0]
cucumbers = []
xmax, ymax = 0, 0
with open(fileName) as lines:
for y, line in enumerate(lines):
for x, cucumber in enumerate(line.strip()):
if cucumber == ".": continue
cucumbers.append(Cucumber(x, y, cucumber == ">"))
xmax = max(xmax, x)
ymax = max(ymax, y)
steps = 1
while moving(cucumbers, xmax, ymax):
steps += 1
print(f"Stopping after {steps} steps")
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))