-
Notifications
You must be signed in to change notification settings - Fork 3
/
day08.py
executable file
·74 lines (62 loc) · 1.91 KB
/
day08.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
#!/usr/bin/env python
import itertools
flatten = itertools.chain.from_iterable
def load(filename, width, height):
with open(filename, "r") as myfile:
data = myfile.read().strip()
# num_planes = len(data) // (width * height)
x = 0
y = 0
z = 0
row = []
rows = []
planes = []
for char in data:
row.append(int(char))
x += 1
if x >= width:
rows.append(row)
row = []
x = 0
y += 1
if y >= height:
planes.append(rows)
rows = []
y = 0
z += 1
print(f"Number of planes: {len(planes)}")
return planes
def flat_count(plane, target):
""" Given a plane (2d array) and a target number, how many times
does that number appear in that plane? """
return list(flatten(plane)).count(target)
def part1(planes):
plane_min_zeros = min(planes, key=lambda plane: flat_count(plane, 0))
return flat_count(plane_min_zeros, 1) * flat_count(plane_min_zeros, 2)
def part2(planes):
num_planes = len(planes)
num_y = len(planes[0])
num_x = len(planes[0][0])
for y in range(num_y):
for x in range(num_x):
char = "?"
for z in range(num_planes):
this_char = planes[z][y][x]
if this_char in (0, 1):
char = this_char
break
if char == 1:
print("#", end="")
elif char == 0:
print(" ", end="")
else:
print(char, end="")
print("")
print(f"planes: {num_planes} y: {num_y} x: {num_x}")
if __name__ == "__main__":
# a1 = load("./input_small.txt", 3, 2)
a1 = load("../input.txt", 25, 6)
print("Part1: ")
print(part1(a1))
print("Part2: ")
part2(a1)