-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.py
165 lines (144 loc) · 5.38 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
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
lines = [x.strip() for x in open('17.input', 'r').readlines() if x != '']
lines = [x.strip() for x in open('17.test', 'r').readlines() if x != '']
# My solution. Here are other for more inspiration:
# https://github.com/r0mainK/advent/blob/master/2020/17.py
# Numpy: https://gist.github.com/sciyoshi/3b0df201d087b4e5beda8a015e7f3699
# Convolution: https://www.reddit.com/r/adventofcode/comments/keqsfa/2020_day_17_solutions/gg45jot?utm_source=share&utm_medium=web2x&context=3
# Solutions: Either keep track of neighbours coordinates. Or use a space matrix
# approach with 1 and 0 to calculate. Or use numpy
def part1(lines, verbose=False):
neighbours = []
for y, line in enumerate(lines):
for x, letter in enumerate(line):
if letter == '#':
neighbours.append((x,y,0))
def get_neighbours(neighbours, x,y,z):
return [
n
for n in neighbours
if
n != (x,y,z) and
x-1 <= n[0] <= x+1 and
y-1 <= n[1] <= y+1 and
z-1 <= n[2] <= z+1
]
def get_range(coordinates, OFFSET=1):
xs = [c[0] for c in coordinates]
ys = [c[1] for c in coordinates]
zs = [c[2] for c in coordinates]
return (
min(xs) - OFFSET, max(xs) + OFFSET,
min(ys) - OFFSET, max(ys) + OFFSET,
min(zs) - OFFSET, max(zs) + OFFSET,
)
def get_spaces(neighbours):
xmin, xmax, ymin, ymax, zmin, zmax = get_range(neighbours)
spaces = []
for z in range(zmin, zmax+1):
for y in range(ymin, ymax+1):
for x in range(xmin, xmax+1):
spaces.append((x,y,z))
return spaces
def print_map(neighbours):
if not verbose:
return
xmin, xmax, ymin, ymax, zmin, zmax = get_range(neighbours, 0)
for z in range(zmin, zmax+1):
print "z = %d" % z
for y in range(ymin, ymax+1):
line = ""
for x in range(xmin, xmax+1):
ch = "#" if (x,y,z) in neighbours else "."
line = line + ch
print line
lines.append(line)
print ""
ROUNDS = 6
print_map(neighbours)
for i in range(ROUNDS):
new_neighbours = []
spaces = get_spaces(neighbours)
for (x,y,z) in spaces:
n = get_neighbours(neighbours, x,y,z)
if (x,y,z) in neighbours:
if len(n) == 2 or len(n) == 3:
new_neighbours.append((x,y,z))
else:
if len(n) == 3:
new_neighbours.append((x,y,z))
neighbours = new_neighbours
print_map(neighbours)
print "Round %d: %d" % ((i+1), len(neighbours))
print "Solution part 1:", len(neighbours)
def part2(lines, verbose=False):
neighbours = []
for y, line in enumerate(lines):
for x, letter in enumerate(line):
if letter == '#':
neighbours.append((x,y,0,0))
def get_neighbours(neighbours, x,y,z,w):
return [
n
for n in neighbours
if
n != (x,y,z,w) and
x-1 <= n[0] <= x+1 and
y-1 <= n[1] <= y+1 and
z-1 <= n[2] <= z+1 and
w-1 <= n[3] <= w+1
]
def get_range(coordinates, OFFSET=1):
xs = [c[0] for c in coordinates]
ys = [c[1] for c in coordinates]
zs = [c[2] for c in coordinates]
ws = [c[3] for c in coordinates]
return (
min(xs) - OFFSET, max(xs) + OFFSET,
min(ys) - OFFSET, max(ys) + OFFSET,
min(zs) - OFFSET, max(zs) + OFFSET,
min(ws) - OFFSET, max(ws) + OFFSET,
)
def get_spaces(neighbours):
xmin, xmax, ymin, ymax, zmin, zmax, wmin, wmax = get_range(neighbours)
spaces = []
for w in range(wmin, wmax+1):
for z in range(zmin, zmax+1):
for y in range(ymin, ymax+1):
for x in range(xmin, xmax+1):
spaces.append((x,y,z,w))
return spaces
def print_map(neighbours):
if not verbose:
return
xmin, xmax, ymin, ymax, zmin, zmax, wmin, wmax = get_range(neighbours, 0)
for w in range(wmin, wmax+1):
for z in range(zmin, zmax+1):
print "z = %d, w = %d" % (z, w)
for y in range(ymin, ymax+1):
line = ""
for x in range(xmin, xmax+1):
ch = "#" if (x,y,z,w) in neighbours else "."
line = line + ch
print line
lines.append(line)
print ""
ROUNDS = 6
print_map(neighbours)
print '---------'
for i in range(ROUNDS):
new_neighbours = []
spaces = get_spaces(neighbours)
for (x,y,z,w) in spaces:
n = get_neighbours(neighbours, x,y,z,w)
if (x,y,z,w) in neighbours:
if len(n) == 2 or len(n) == 3:
new_neighbours.append((x,y,z,w))
else:
if len(n) == 3:
new_neighbours.append((x,y,z,w))
neighbours = new_neighbours
print_map(neighbours)
print "Round %d count: %d" % ((i+1), len(neighbours))
print "Solution part 2:", len(neighbours)
part1(lines, True)
#part2(lines, False)