-
Notifications
You must be signed in to change notification settings - Fork 0
/
q08.py
69 lines (58 loc) · 1.95 KB
/
q08.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
from enum import Enum
from util import slurp, split_every
from itertools import starmap
def part1():
"""
image is split into layers of width*height
find layer with smallest number of zeros
return number of 1s * number of 2s
"""
image = slurp('inputs/q08')
width, height = 25, 6
layers = split_every(width * height, image)
fewestZeroLayer = min(layers, key=lambda l: l.count('0'))
print(fewestZeroLayer.count('1') * fewestZeroLayer.count('2'))
def part2():
"""
stack the layers: first layer in front, last layer in back
0 = black, 1 = white, 2 = transparent
therefore black/white overlap everything, and transparent overlaps nothing
"""
image = slurp('inputs/q08')
width, height = 25, 6
combined = combineLayers(image, width, height)
print(layerToImage(combined, width))
def part2_test():
test = "0222112222120000"
print(''.join(combineLayers(test, 2, 2)))
class Color(Enum):
BLACK = '0'
WHITE = '1'
TRANSPARENT = '2'
def combineLayers(image, width, height):
layers = split_every(width * height, image)
# Start with transparent canvas
canvas = Color.TRANSPARENT.value * (width * height)
def applyPixel(oldValue, newValue):
""" apply newValue behind oldValue """
if oldValue == Color.TRANSPARENT.value:
return newValue
else:
return oldValue
for layer in layers:
canvas = starmap(applyPixel, zip(canvas, layer))
return canvas
def layerToImage(image, width):
"""
images are stored as a long string of chars. this function adds newlines
so the image can be printed and viewed by a human, and removes transparent
pixels
"""
image = '\n'.join(''.join(chunk) for chunk in split_every(width, image))
# Remove transparent pixels
image = image.replace('2', ' ')
# Make image more readable
image = image.replace('0', '.')
image = image.replace('1', '#')
return image
part2()