advent-of-code-2019/day08.py

47 lines
1.0 KiB
Python

#!/usr/bin/env python3
from lib import Color
ZERO, ONE, TWO = "012"
BLACK = ZERO
WHITE = ONE
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(BLACK, Color.BLACK).replace(WHITE, 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)