advent-of-code-2019/day08.py

50 lines
1.1 KiB
Python

#!/usr/bin/env python3
ZERO, ONE, TWO = "012"
BLACK = 0
WHITE = 1
color = {WHITE : u"\u2B1C",
BLACK : u"\u2B1B"
}
class Image:
def __init__(self, data, w, h):
self.data = data
self.w = w
self.h = h
def layers(self):
w, h = self.w, self.h
return (self.data[i:i+w*h] for i in range(0, len(self.data), w*h))
def image(self):
final = str()
for stack in zip(*self.layers()):
final += (''.join(stack).replace(TWO, "") + TWO)[0]
return final
def __repr__(self):
w, image = self.w, self.image()
return '\n'.join(image[i:i+w] for i in range(0, len(image), w))
def __str__(self):
return repr(self).replace(ZERO, color[BLACK]).replace(ONE, color[WHITE])
def preproc(puzzle_input):
w, h = 25, 6
data = puzzle_input.replace('\n', '')
return Image(data, w, h)
def partI(image):
layer = min(image.layers(), key=lambda ly: ly.count(ZERO))
return layer.count(ONE) * layer.count(TWO)
def partII(image):
return '\n' + str(image)