-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.py
80 lines (62 loc) · 1.83 KB
/
day8.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
79
80
width = 25
height = 6
layers = []
grid = [["-" for i in range(width)] for j in range(height)]
for i in range(height): print(grid[i])
fileName = "./inputs/day8.txt"
f = open(fileName, "r")
fileInput = f.read()
f.close()
noOfLayers = int(len(fileInput) / (width*height))
for i in range(noOfLayers): layers.append(grid)
zerosInLayer = [0 for i in range(noOfLayers)]
onesInLayer = [0 for i in range(noOfLayers)]
twosInLayer = [0 for i in range(noOfLayers)]
for x in range(noOfLayers):
#print("Layer", x)
startOfLayer = x*width*height
for y in range(height):
for z in range(width):
pos = startOfLayer + y*width + z
#print("Position", pos,"(",x,y,z, ") has value", fileInput[pos])
layers[x][y][z] = fileInput[pos]
if fileInput[pos] == "0": zerosInLayer[x] += 1
if fileInput[pos] == "1": onesInLayer[x] += 1
if fileInput[pos] == "2": twosInLayer[x] += 1
minLayer = zerosInLayer.index(min(zerosInLayer))
print("Layer with least zeros is", zerosInLayer.index(min(zerosInLayer)))
print("Part 1 answer", onesInLayer[minLayer]*twosInLayer[minLayer])
def printImage(pic):
for y in range(height):
print()
for z in range(width):
if pic[y][z] == "1":
#print(pic[y][z], end='')
print("1", end='')
elif pic[y][z] == "-":
print("-", end='')
elif pic[y][z] == "2":
print("?", end='')
else:
print(" ", end='')
print()
its = 0
picture = [["-" for i in range(width)] for j in range(height)]
printImage(picture)
for z in range(width):
for y in range(height):
print(y,z)
layer = 0
while picture[y][z] == "-":
pos = layer*height*width + z*height + y
print(fileInput[pos])
if layer == 99:
picture[y][z] = fileInput[pos]
if fileInput[pos] == "0" or fileInput[pos] == "1":
print("Found in layer", layer)
picture[y][z] = fileInput[pos]
else:
layer += 1
print(its)
printImage(picture)
print()