-
Notifications
You must be signed in to change notification settings - Fork 1
/
20.py
88 lines (62 loc) · 2.02 KB
/
20.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
79
80
81
82
83
84
85
86
87
88
import fileinput
import copy
def parse():
algorithm = None
grid = set()
for r, l in enumerate(fileinput.input()):
l = l.strip()
if not algorithm:
algorithm = l
elif not l:
continue
else:
for c, v in enumerate(l):
if v == '#':
grid.add((r, c))
return algorithm, grid
dr = [-1, -1, -1, 0, 0, 0, 1, 1, 1]
dc = [-1, 0, 1, -1, 0, 1, -1, 0, 1]
def play(round, algorithm, grid):
G = set()
max_r = max([r for r, c in grid])
min_r = min([r for r, c in grid])
max_c = max([c for r, c in grid])
min_c = min([c for r, c in grid])
for r in range(min_r - 5, max_r + 10):
for c in range(min_c - 5, max_c + 10):
binary = ''
for i in range(len(dr)):
rr = dr[i] + r
cc = dc[i] + c
if (rr, cc) in grid:
binary += '1'
elif min_r <= rr <= max_r and min_c <= cc <= max_c:
# In the grid, but dark
binary += '0'
else:
# Out of the grid, it depends on what round it is
# symbol = algorithm[0] if round % 2 == 0 else algorithm[-1]
# binary += '1' if symbol == '#' else '0'
binary += str(round % 2)
if algorithm[int(binary, 2)] == '#':
G.add((r, c))
return G
def task():
algorithm, grid = parse()
for round in range(50):
grid = play(round, algorithm, grid)
if round == 1:
print(len(grid))
print_grid(grid)
return len(grid)
def print_grid(grid):
max_r = max([r for r, c in grid])
min_r = min([r for r, c in grid])
max_c = max([c for r, c in grid])
min_c = min([c for r, c in grid])
for r in range(min_r - 5, max_r + 5):
row = []
for c in range(min_c - 5, max_c + 5):
row.append('#' if (r, c) in grid else '.')
print(''.join(row))
print(task())