-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.py
68 lines (56 loc) · 1.81 KB
/
18.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
from lib import *
input = read_input(2018, 18)
def cntAdj(grid, y, x, t):
return sum(
grid[i][j] == t
for i in range(y - 1, y + 2)
for j in range(x - 1, x + 2)
if (i, j) != (y, x) and i in range(len(grid)) and j in range(len(grid[i]))
)
grid = input.splitlines()
for _ in range(10):
new_grid = []
for i, line in enumerate(grid):
new_line = ""
for j, c in enumerate(line):
if c == "." and cntAdj(grid, i, j, "|") >= 3:
new_line += "|"
elif c == "|" and cntAdj(grid, i, j, "#") >= 3:
new_line += "#"
elif c == "#" and not (cntAdj(grid, i, j, "#") >= 1 and cntAdj(grid, i, j, "|") >= 1):
new_line += "."
else:
new_line += c
new_grid.append(new_line)
grid = new_grid
a = sum(line.count("|") for line in grid)
b = sum(line.count("#") for line in grid)
print(a * b)
def next_iteration(grid):
new_grid = []
for i, line in enumerate(grid):
new_line = ""
for j, c in enumerate(line):
if c == "." and cntAdj(grid, i, j, "|") >= 3:
new_line += "|"
elif c == "|" and cntAdj(grid, i, j, "#") >= 3:
new_line += "#"
elif c == "#" and not (cntAdj(grid, i, j, "#") >= 1 and cntAdj(grid, i, j, "|") >= 1):
new_line += "."
else:
new_line += c
new_grid.append(new_line)
return new_grid
grid = input.splitlines()
seen = {}
i = 0
while tuple(grid) not in seen:
seen[tuple(grid)] = i
grid = next_iteration(grid)
i += 1
cycle = i - seen[tuple(grid)]
for _ in range((1000000000 - i) % cycle):
grid = next_iteration(grid)
a = sum(line.count("|") for line in grid)
b = sum(line.count("#") for line in grid)
print(a * b)