-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17_1.py
65 lines (57 loc) · 2.06 KB
/
day17_1.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
class Day17_1:
input = """#.#.#.##
.####..#
#####.#.
#####..#
#....###
###...##
...#.#.#
#.##..##"""
def __init__(self):
grid = self.input.split('\n')
size = 20
for i, _ in enumerate(grid):
a = list(grid[i])
a.insert(0, "." * int(size / 2 - 8))
a.append("." * int(size / 2 - 8))
grid[i] = ''.join(a)
for i in range(int(size / 2 - 8)):
grid.insert(0, "." * size)
grid.append("." * size)
padding = ["." * size for i in range(size)]
space = [padding, grid, padding]
for i in range(6):
new_space = []
for z_i, z in enumerate(space):
new_z = z.copy()
for y_i, y in enumerate(z):
for x_i, x in enumerate(y):
adjacent_active = self.count_adjacent_active(x_i, y_i, z_i, space)
if x == '#':
if adjacent_active < 2 or adjacent_active > 3:
new_z[y_i] = new_z[y_i][:x_i] + '.' + new_z[y_i][x_i + 1:]
elif x == '.':
if adjacent_active == 3:
new_z[y_i] = new_z[y_i][:x_i] + '#' + new_z[y_i][x_i + 1:]
new_space.append(new_z)
new_space.insert(0, padding)
new_space.append(padding)
space = new_space
count = 0
for z in new_space:
for y in z:
count += y.count('#')
print(count)
def count_adjacent_active(self, x_i, y_i, z_i, space):
adjacent_active = 0
dirs = {-1, 0, 1}
for z in [z_i + d for d in dirs]:
for y in [y_i + d for d in dirs]:
for x in [x_i + d for d in dirs]:
try:
if (space[z][y][x] == '#') and not (z == z_i and y == y_i and x == x_i):
adjacent_active += 1
except Exception:
pass
return adjacent_active
Day17_1()