-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.py
87 lines (77 loc) · 3.03 KB
/
17.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
from copy import deepcopy
from itertools import product
from aocd import get_data
data = list(map(list, get_data(day=17, year=2020).splitlines()))
ACTIVE, INACTIVE = "#", "."
def calc_surroundings1(x, y, z):
vectors = list(product([1, 0, -1], repeat=3))
vectors.remove((0, 0, 0))
surroundings = []
for vector in vectors:
surrounding = repr((x + vector[0], y + vector[1], z + vector[2]))
if surrounding not in space:
space[surrounding] = INACTIVE
surroundings.append(space[surrounding])
return surroundings
def calc_surroundings2(x, y, z, w):
vectors = list(product([1, 0, -1], repeat=4))
vectors.remove((0, 0, 0, 0))
surroundings = []
for vector in vectors:
surrounding = repr((x + vector[0], y + vector[1], z + vector[2], w + vector[3]))
if surrounding not in space:
space[surrounding] = INACTIVE
surroundings.append(space[surrounding])
return surroundings
size = len(data)
space = {
repr((x - (size // 2), y - (size // 2), 0)): data[y][x]
for x in range(size)
for y in range(size)
}
space_copy = deepcopy(space)
for _ in range(6):
size += 2
for z in range(-(size // 2), size // 2 + 1):
for y in range(-(size // 2), size // 2 + 1):
for x in range(-(size // 2), size // 2 + 1):
cube = repr((x, y, z))
if cube not in space:
space[cube] = INACTIVE
surroundings = calc_surroundings1(x, y, z)
if space[cube] == ACTIVE and surroundings.count(ACTIVE) not in [
2,
3,
]:
space_copy[cube] = INACTIVE
elif space[cube] == INACTIVE and surroundings.count(ACTIVE) == 3:
space_copy[cube] = ACTIVE
space = deepcopy(space_copy)
print("2020 Day 17")
print("\tPart 1:", list(space.values()).count(ACTIVE))
size = len(data)
space = {
repr((x - (size // 2), y - (size // 2), 0, 0)): data[y][x]
for x in range(size)
for y in range(size)
}
space_copy = deepcopy(space)
for _ in range(6):
size += 2
for w in range(-(size // 2), size // 2 + 1):
for z in range(-(size // 2), size // 2 + 1):
for y in range(-(size // 2), size // 2 + 1):
for x in range(-(size // 2), size // 2 + 1):
cube = repr((x, y, z, w))
if cube not in space:
space[cube] = INACTIVE
surroundings = calc_surroundings2(x, y, z, w)
if space[cube] == ACTIVE and surroundings.count(ACTIVE) not in [
2,
3,
]:
space_copy[cube] = INACTIVE
elif space[cube] == INACTIVE and surroundings.count(ACTIVE) == 3:
space_copy[cube] = ACTIVE
space = deepcopy(space_copy)
print("\tPart 2:", list(space.values()).count(ACTIVE))