-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.py
41 lines (34 loc) · 1.27 KB
/
day17.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
from itertools import product
with open("input/day17", "r") as input:
data = input.read().split("\n")
data.pop()
def parseInput(dimensions):
field = set()
for idx in range(len(data)):
line = data[idx]
for idx2 in range(len(line)):
if line[idx2] == '#':
field.add(tuple([idx2, idx] + [0 for _ in range(dimensions - 2)]))
return field
def parts(dimensions):
field = parseInput(dimensions)
prod = set(product(range(-1, 2), repeat=dimensions))
prod.remove(tuple([0 for i in range(dimensions)]))
for _ in range(6):
numOfActiveNeighbours = {}
for pos in field:
for p in prod:
affectedPos = tuple([pos[i] + p[i] for i in range(dimensions)])
if affectedPos not in numOfActiveNeighbours:
numOfActiveNeighbours[affectedPos] = 1
else:
numOfActiveNeighbours[affectedPos] += 1
nextField = set()
for pos in numOfActiveNeighbours:
val = numOfActiveNeighbours[pos]
if (pos in field and val == 2) or val == 3:
nextField.add(pos)
field = nextField
return str(len(field))
print("Part 1: " + parts(3))
print("Part 2: " + parts(4))