-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.py
executable file
·78 lines (66 loc) · 1.99 KB
/
25.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
from __future__ import annotations
import enum
from typing import Dict, NamedTuple, Optional, Tuple
class Board(NamedTuple):
rows: int
cols: int
east: Set[Tuple[int, int]]
south: Set[Tuple[int, int]]
def _coords(self, row: int, col: int) -> Tuple[int, int]:
return (
row % self.rows,
col % self.cols,
)
def nextgen(self) -> Board:
neast = set()
nsouth = set()
# First, east facing.
for (row, col) in self.east:
nxt = self._coords(row, col + 1)
if nxt in self.east or nxt in self.south:
neast.add((row, col))
else:
neast.add(nxt)
# Then, south facing
for (row, col) in self.south:
nxt = self._coords(row + 1, col)
if nxt in self.south or nxt in neast:
nsouth.add((row, col))
else:
nsouth.add(nxt)
return Board(self.rows, self.cols, neast, nsouth)
@classmethod
def parse(cls, data: str) -> Board:
lines = data.split("\n")[:-1]
east = set()
south = set()
for row, line in enumerate(lines):
for col, char in enumerate(line):
if char == ">":
east.add((row, col))
elif char == "v":
south.add((row, col))
return cls(len(lines), len(lines[0]), east, south)
def __repr__(self) -> str:
res = ""
for row in range(self.rows):
for col in range(self.cols):
if (row, col) in self.east:
res += ">"
elif (row, col) in self.south:
res += "v"
else:
res += "."
res += "\n"
return res
with open("data/25.txt") as f:
data = f.read()
board = Board.parse(data)
nxt = board.nextgen()
i = 1
while board != nxt:
board = nxt
nxt = board.nextgen()
i += 1
print(i)