Intcode as a single file, add SPDX-License-Identifier

This commit is contained in:
Akumatic
2020-12-02 21:34:21 +01:00
parent 4ec62b1cdb
commit 10c048ab41
17 changed files with 386 additions and 387 deletions

View File

@ -1,25 +1,29 @@
""" https://adventofcode.com/2019/day/8 """
# SPDX-License-Identifier: MIT
# Copyright (c) 2019 Akumatic
#
# https://adventofcode.com/2019/day/8
def readFile():
def readFile() -> str:
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
return f.read()[:-1]
def getLayers(input, width, height):
def getLayers(input: str, width: int, height: int) -> list:
layers = []
for i in range(0, len(input), width*height):
layers.append([input[i+width*x:i+width*(x+1)] for x in range(height)])
return layers
def getPicture(layers):
def getPicture(layers: list) -> str:
width, height = len(layers[0][0]), len(layers[0])
return "\n".join(["".join([getColor(layers, w, h) for w in range(width)]) for h in range(height)])
def getColor(layers, w, h):
def getColor(layers: list, w: int, h: int) -> str:
for layer in layers:
if layer[h][w] != "2": return layer[h][w]
if layer[h][w] != "2":
return layer[h][w]
return "2"
def part1(layers):
def part1(layers: list) -> int:
min, minLayer = None, None
for layer in layers:
cnt = sum([l.count("0") for l in layer])
@ -27,7 +31,7 @@ def part1(layers):
min, minLayer = cnt, layer
return sum([l.count("1") for l in minLayer]) * sum([l.count("2") for l in minLayer])
def part2(layers):
def part2(layers: list) -> str:
picture = getPicture(layers)
return f"\n{picture.replace('0', ' ').replace('1', 'X')}"