-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart-1.py
78 lines (52 loc) · 1.56 KB
/
part-1.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
# Day 11 : Dumbo Octopus
## Part 1
import numpy as np
from scipy.ndimage.morphology import binary_dilation
from scipy.ndimage import generate_binary_structure
from functools import partial
# define dilation
selem = generate_binary_structure(2, 2)
dilate = partial(binary_dilation, structure=selem)
# define class to combine flashes
class FlashMask:
def __init__(self, shape):
self.shape = shape
self.array = np.zeros(shape)
self.count = 0
def __add__(self, array):
return array + self.array
def __new_mask(self, i, j):
mask = np.zeros(self.shape)
mask[i, j] = 1
return dilate(mask)
def add(self, i, j):
mask = self.__new_mask(i, j)
self.array += mask
self.count += 1
def reset(self):
self.array = np.zeros(self.shape)
# load octopi
octopi = open("octopi.txt").read().split("\n")
octopi = np.array([list(o) for o in octopi], dtype=int)
# initialise flash mask
flash_mask = FlashMask(octopi.shape)
# iterate days
n_days = 100
for day in range(n_days):
# Add 1 to all
octopi += 1
# find flashes
flashes = octopi > 9
# while there are more to flash, keep flashing!
while np.sum(flashes) > 0:
# Add flashes
for i, j in np.argwhere(flashes):
flash_mask.add(i, j)
octopi = flash_mask + octopi
# update params
octopi[flashes] = -np.inf
flashes = octopi > 9
flash_mask.reset()
# Set flashers to 0 energy
octopi[octopi == -np.inf] = 0
print(flash_mask.count)