-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
56 lines (40 loc) · 1.23 KB
/
8.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
import numpy as np
from matplotlib import pyplot as plt
def countDigits(layer):
digits = {}
for digit in layer:
digits[digit] = 1 if (digit not in digits.keys()) else digits[digit] + 1
return digits
def getLayerWithFewestZeroes(layers):
digit_tallies = [
{
"index": i,
"digits": countDigits(layer)
} for (i, layer) in enumerate(layers)
]
digit_tallies.sort(key=lambda x: x["digits"][0])
layer_index = digit_tallies[0]["index"]
return layers[layer_index]
if __name__ == "__main__":
width = 25
height = 6
with open("input/8.input", "r") as file:
input_string = file.read()
image_data = [int(digit) for digit in input_string]
assert len(image_data) % (width * height) == 0
layers = np.reshape(image_data, (-1, width * height)).tolist()
# Part 1
part_1_layer = getLayerWithFewestZeroes(layers)
part_1_digits = countDigits(part_1_layer)
part_1_result = part_1_digits[1] * part_1_digits[2]
print("Part 1 | result of multiplying # of digits:", part_1_result)
# Part 2
image = np.ones((height, width)) * 2
mask = np.ones((height, width))
for layer in layers:
image += mask * (np.reshape(layer, (height, width)) - 2)
mask = (image == 2) * 1
if not mask.any():
break
plt.imshow(image)
plt.show()