Add solution for day 8 part 2

master
Nick Krichevsky 2019-12-08 01:22:00 -05:00
parent 307f800c98
commit 19f3da86c6
2 changed files with 30 additions and 0 deletions

View File

@ -14,12 +14,38 @@ def make_layers(input: Tuple[int], width: int, height: int) -> Tuple[Tuple[int]]
return tuple(layers) return tuple(layers)
def stack_layers(layers: Tuple[Tuple[str]], width: int, height: int) -> Tuple[int]:
merged_stacks = []
for i in range(height):
for j in range(width):
stack = []
for layer in layers:
stack.append(layer[i * width + j])
try:
pixel = next(item for item in stack if item != 2)
except StopIteration:
pixel = 0
merged_stacks.append(pixel)
return tuple(merged_stacks)
def part1(layers: Tuple[Tuple[str]]) -> int: def part1(layers: Tuple[Tuple[str]]) -> int:
fewest_zero_layer = min((layer for layer in layers), key=lambda layer: layer.count(0)) fewest_zero_layer = min((layer for layer in layers), key=lambda layer: layer.count(0))
return fewest_zero_layer.count(1) * fewest_zero_layer.count(2) return fewest_zero_layer.count(1) * fewest_zero_layer.count(2)
def part2(layers: Tuple[Tuple[str]], width: int, height: int) -> int:
stacked = stack_layers(layers, width, height)
for i in range(height):
for j in range(width):
pixel = stacked[i * width + j]
print('#' if pixel == 1 else ' ', end=' ')
print('')
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) != 4: if len(sys.argv) != 4:
print("Usage: ./main.py in_file width height") print("Usage: ./main.py in_file width height")
@ -32,4 +58,7 @@ if __name__ == "__main__":
height = int(sys.argv[3]) height = int(sys.argv[3])
layers = make_layers(input, width, height) layers = make_layers(input, width, height)
print("PART 1")
print(part1(layers)) print(part1(layers))
print("PART 2")
part2(layers, width, height)

1
day8/sample2.txt Normal file
View File

@ -0,0 +1 @@
0222112222120000