-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
163 lines (147 loc) · 3.88 KB
/
day11.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from collections import Counter
from copy import deepcopy
from itertools import chain
def adjacent(m, i, j):
nrows = len(m)
ncols = len(m[0])
adj = [(i-1, j-1), (i-1, j), (i-1, j+1),
(i+1, j-1), (i+1, j), (i+1, j+1),
(i, j-1), (i, j+1)]
return [m[x][y] for x, y in adj if 0 <= x < nrows and 0 <= y < ncols]
def up_right(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
ii = i-t;
if ii < 0: return None
jj = j+t;
if jj > ncols-1: return None
x = m[ii][jj];
if x in ('L','#'): return x
return None
def up_left(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
ii = i-t;
if ii < 0: return None
jj = j-t;
if jj < 0: return None
x = m[ii][jj];
if x in ('L','#'): return x
return None
def down_right(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
ii = i+t;
if ii > nrows-1: return None
jj = j+t;
if jj > ncols-1: return None
x = m[ii][jj];
if x in ('L','#'): return x
return None
def down_left(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
ii = i+t;
if ii > nrows-1: return None
jj = j-t;
if jj < 0: return None
x = m[ii][jj];
if x in ('L','#'): return x
return None
def right(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
jj = j+t;
if jj > ncols-1: return None
x = m[i][jj];
if x in ('L','#'): return x
return None
def left(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
jj = j-t;
if jj < 0: return None
x = m[i][jj];
if x in ('L','#'): return x
return None
def down(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
ii = i+t;
if ii > nrows-1: return None
x = m[ii][j];
if x in ('L','#'): return x
return None
def up(m, i, j):
nrows = len(m)
ncols = len(m[0])
for t in range(1,nrows):
ii = i-t;
if ii < 0: return None
x = m[ii][j];
if x in ('L','#'): return x
return None
def get_vis(m, i, j):
res = []
res.append(up(m,i,j))
res.append(down(m,i,j))
res.append(right(m,i,j))
res.append(left(m,i,j))
res.append(up_right(m,i,j))
res.append(up_left(m,i,j))
res.append(down_right(m,i,j))
res.append(down_left(m,i,j))
return [x for x in res if x is not None]
def change1(mat):
nrows = len(mat)
ncols = len(mat[0])
new_mat = deepcopy(mat)
for i in range(nrows):
for j in range(ncols):
adj = adjacent(mat, i, j)
c = Counter(adj)
if mat[i][j] == 'L' and c['#'] == 0:
new_mat[i][j] = '#'
elif mat[i][j] == '#' and c['#'] >= 4:
new_mat[i][j] = 'L'
return new_mat
def change2(mat):
nrows = len(mat)
ncols = len(mat[0])
new_mat = deepcopy(mat)
for i in range(nrows):
for j in range(ncols):
adj = get_vis(mat, i, j)
c = Counter(adj)
if mat[i][j] == 'L' and c['#'] == 0:
new_mat[i][j] = '#'
elif mat[i][j] == '#' and c['#'] >= 5:
new_mat[i][j] = 'L'
return new_mat
def run(m1, change):
while True:
m2 = change(m1)
if m1 == m2:
break
else:
m1 = m2
c = Counter(chain.from_iterable(m2))
return c['#']
def main(inp):
with open(inp) as f:
lines = f.read().splitlines()
mat = [[c for c in line] for line in lines]
res1 = run(mat, change1)
res2 = run(mat, change2)
return res1, res2
if __name__ == '__main__':
res1, res2 = main('../input/input11.txt')
print('day11.1:', res1)
print('day11.2:', res2)