-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2019_08.py
64 lines (47 loc) · 1.64 KB
/
AoC2019_08.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
#! /usr/bin/env python3
#
# Advent of Code 2019 Day 8
#
import sys
from typing import cast
from advent_of_code_ocr import convert_6
from aoc.common import InputData
from aoc.common import SolutionBase
WIDTH = 25
HEIGHT = 6
Input = str
Output1 = int
Output2 = str
class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return list(input_data)[0]
def get_layers(self, input: str, width: int, height: int) -> list[str]:
layer_size = width * height
return [
input[i : i + layer_size] # noqa E203
for i in range(0, len(input), layer_size)
]
def part_1(self, input: str) -> int:
layers = self.get_layers(input, WIDTH, HEIGHT)
least_zeroes = min(layers, key=lambda lyr: lyr.count("0"))
return least_zeroes.count("1") * least_zeroes.count("2")
def get_image(self, input: str, width: int, height: int) -> str:
layers = self.get_layers(input, width, height)
return "".join(
next(lyr[i] for lyr in layers if lyr[i] != "2")
for i in range(0, len(layers[0]))
)
def part_2(self, input: str) -> str:
image = self.get_image(input, WIDTH, HEIGHT)
to_ocr = "\n".join(
image[i : i + WIDTH] # noqa E203
for i in range(0, WIDTH * HEIGHT, WIDTH)
)
return cast(str, convert_6(to_ocr, fill_pixel="1", empty_pixel="0"))
def samples(self) -> None:
assert self.get_image("0222112222120000", 2, 2) == "0110"
solution = Solution(2019, 8)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()