-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11.py
More file actions
80 lines (60 loc) · 2.09 KB
/
Day11.py
File metadata and controls
80 lines (60 loc) · 2.09 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
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
import Utility
filePath = 'input/day11/part1.txt'
class OctopusSwarm:
def __init__(self) -> None:
inputLines = Utility.getLinesFromFile(filePath)
self.octopuses = [[0 for i in range(10)] for i in range(10)]
self.totalFlashes = 0
for i, line in enumerate(inputLines):
for j, c in enumerate(line):
self.octopuses[i][j] = int(c)
def stepN(self, n):
for i in range(n):
self.step()
def findFullFlashStep(self):
for i in range(1, 10000000):
if(self.step()):
return i
def step(self):
flashesThisStep = 0
flashing = []
for y in range(10):
for x in range(10):
self.octopuses[y][x] += 1
if(self.octopuses[y][x] == 10):
flashing.append((y, x))
while(len(flashing) > 0):
self.totalFlashes += 1
flashesThisStep += 1
fY, fX = flashing.pop()
neighbors = self.getNeighbors(fY, fX)
for nY, nX in neighbors:
self.octopuses[nY][nX] += 1
if (self.octopuses[nY][nX] == 10):
flashing.append((nY, nX))
for y in range(10):
for x in range(10):
if(self.octopuses[y][x] > 9):
self.octopuses[y][x] = 0
return flashesThisStep == 100
def getNeighbors(self, y, x):
possibleNeighbors = []
for xd in range(-1, 2):
for yd in range(-1, 2):
if(xd == 0 and yd == 0):
continue
possibleNeighbors.append((y + yd, x + xd))
return list(filter(lambda c: c[1] >= 0 and c[1] < 10 and c[0] >= 0 and c[0] < 10, possibleNeighbors))
def solvePart1():
swarm = OctopusSwarm()
swarm.stepN(100)
print('Solution to part1:')
print(swarm.totalFlashes)
def solvePart2():
swarm = OctopusSwarm()
fullFlashStep = swarm.findFullFlashStep()
print('Solution to part2:')
print(fullFlashStep)
if(__name__ == '__main__'):
solvePart1()
solvePart2()