-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (137 loc) · 4.63 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from src.common.file_utils import get_path, read_lines
from collections import defaultdict
ALIVE = "#"
DEAD = "."
class Board():
def __init__(self, input_board):
self.cells = self.init_cells(input_board)
def init_cells(self, board):
cells = {}
h = len(board)
w = len(board[0])
for r in range(h):
for c in range(w):
cells[(c, r, 0)] = board[r][c]
return cells
def print(self):
max_z, min_z, max_y, max_x = 0, 0, 0, 0
for (x, y, z) in self.cells:
max_x = max(max_x, x)
max_y = max(max_y, y)
max_z = max(max_z, z)
min_z = min(min_z, z)
s = ""
for z in range(min_z, max_z + 1):
s += "Z = {}\n".format(z)
for y in range(0, max_y + 1):
for x in range(0, max_x + 1):
s += self.cells.get((x, y, z), DEAD)
s += "\n"
s += "\n"
print(s)
def cycle(self):
new_neighbours = defaultdict(int)
for cell, val in self.cells.items():
if val is ALIVE:
neighbours = get_neighbours_array_1(cell)
for n in neighbours:
new_neighbours[n] += 1
new_cells = {}
for cell, count in new_neighbours.items():
if (cell in self.cells and self.cells[cell] == ALIVE) and (2 <= count <= 3):
new_cells[cell] = ALIVE
elif (cell not in self.cells or self.cells[cell] == DEAD) and (count == 3):
new_cells[cell] = ALIVE
self.cells = new_cells
def count_neighbours(self, loc):
n = 0
for neighbour in get_neighbours_array_1(loc):
if self.cells.get(neighbour, DEAD) == ALIVE:
n += 1
return n
def count_alive(self):
alive = 0
for state in self.cells.values():
if state == ALIVE:
alive += 1
return alive
def get_neighbours_array_1(loc):
n = []
x, y, z = loc
for dz in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if [dx, dy, dz] != [0, 0, 0]:
n.append((x + dx, y + dy, z + dz))
return n
# TODO: Remove big duplication between 1 and 2
class Board_2():
def __init__(self, input_board):
self.cells = self.init_cells(input_board)
def init_cells(self, board):
cells = {}
h = len(board)
w = len(board[0])
for r in range(h):
for c in range(w):
cells[(c, r, 0, 0)] = board[r][c]
return cells
def cycle(self):
new_neighbours = defaultdict(int)
for cell, val in self.cells.items():
if val is ALIVE:
neighbours = get_neighbours_array_2(cell)
for n in neighbours:
new_neighbours[n] += 1
new_cells = {}
for cell, count in new_neighbours.items():
if (cell in self.cells and self.cells[cell] == ALIVE) and (2 <= count <= 3):
new_cells[cell] = ALIVE
elif (cell not in self.cells or self.cells[cell] == DEAD) and (count == 3):
new_cells[cell] = ALIVE
self.cells = new_cells
def count_neighbours(self, loc):
n = 0
for neighbour in get_neighbours_array_2(loc):
if self.cells.get(neighbour, DEAD) == ALIVE:
n += 1
return n
def count_alive(self):
alive = 0
for state in self.cells.values():
if state == ALIVE:
alive += 1
return alive
def get_neighbours_array_2(loc):
n = []
x, y, z, w = loc
for dw in [-1, 0, 1]:
for dz in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if [dx, dy, dz, dw] != [0, 0, 0, 0]:
n.append((x + dx, y + dy, z + dz, w + dw))
return n
def part_one(filename: str) -> int:
lines = read_lines(get_path(__file__, filename))
board = Board(lines)
for i in range(6):
# print("ROUND: {}".format(i))
board.cycle()
# board.print()
return board.count_alive()
def part_two(filename: str) -> int:
lines = read_lines(get_path(__file__, filename))
board = Board_2(lines)
for i in range(6):
# print("ROUND: {}".format(i))
board.cycle()
# board.print()
return board.count_alive()
if __name__ == '__main__':
print("---Part One---")
part_one_res = part_one("input.txt")
print(part_one_res)
print("---Part Two---")
part_two_res = part_two("input.txt")
print(part_two_res)