-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpartOne.py
More file actions
50 lines (39 loc) · 1.53 KB
/
partOne.py
File metadata and controls
50 lines (39 loc) · 1.53 KB
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
from common import *
from typing import Dict, Tuple
def count_neighbours(
matrix: Dict[Tuple[int, int, int], str], position: Tuple[int, int, int]
) -> int:
num_neighbours = 0
x, y, z = position
for (x_delta, y_delta, z_delta) in translation_vectors_3d:
if (
matrix.get((x + x_delta, y + y_delta, z + z_delta), inactive_marker)
== active_marker
):
num_neighbours += 1
return num_neighbours
def iterate(matrix: Dict[Tuple[int, int, int], str]) -> Dict[Tuple[int, int, int], str]:
new = {}
# get min/max for x, y and z values
keys = list(matrix)
_, _, min_z = min(keys, key=lambda x: x[2])
_, _, max_z = max(keys, key=lambda x: x[2])
_, min_y, _ = min(keys, key=lambda x: x[1])
_, max_y, _ = max(keys, key=lambda x: x[1])
min_x, _, _ = min(keys, key=lambda x: x[0])
max_x, _, _ = max(keys, key=lambda x: x[0])
for z in range(min_z - 2, max_z + 2):
for y in range(min_y - 2, max_y + 2):
for x in range(min_x - 2, max_x + 2):
num_neighbours = count_neighbours(matrix, (x, y, z))
current_state = matrix.get((x, y, z), inactive_marker)
if (
num_neighbours == 2 and current_state == active_marker
) or num_neighbours == 3:
new[(x, y, z)] = active_marker
return new
def partOne(instr: str) -> int:
matrix = parse(instr, lambda x, y: (x, y, 0))
for _ in range(6):
matrix = iterate(matrix)
return len(matrix)