Added 2019 day 08, modified READMEs

This commit is contained in:
Akumatic 2019-12-08 14:33:52 +01:00
parent 80cacac9bb
commit af2586bd48
7 changed files with 131 additions and 3 deletions

View File

@ -1,5 +1,5 @@
# Advent of Code - 2018 # Advent of Code - 2018
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2018) #### [Direct Link](https://adventofcode.com/2018)
## Intro ## Intro

67
2019/08/README.md Normal file
View File

@ -0,0 +1,67 @@
# 2019 Day 8: Space Image Format
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2019/day/8)
## Part 1
The Elves' spirits are lifted when they realize you have an opportunity to reboot one of their Mars rovers, and so they are curious if you would spend a brief sojourn on Mars. You land your ship near the rover.
When you reach the rover, you discover that it's already in the process of rebooting! It's just waiting for someone to enter a [BIOS](https://en.wikipedia.org/wiki/BIOS) password. The Elf responsible for the rover takes a picture of the password (your puzzle input) and sends it to you via the Digital Sending Network.
Unfortunately, images sent via the Digital Sending Network aren't encoded with any normal encoding; instead, they're encoded in a special Space Image Format. None of the Elves seem to remember why this is the case. They send you the instructions to decode it.
Images are sent as a series of digits that each represent the color of a single pixel. The digits fill each row of the image left-to-right, then move downward to the next row, filling rows top-to-bottom until every pixel of the image is filled.
Each image actually consists of a series of identically-sized layers that are filled in this way. So, the first digit corresponds to the top-left pixel of the first layer, the second digit corresponds to the pixel to the right of that on the same layer, and so on until the last digit, which corresponds to the bottom-right pixel of the last layer.
For example, given an image `3` pixels wide and `2` pixels tall, the image data `123456789012` corresponds to the following image layers:
```
Layer 1: 123
456
Layer 2: 789
012
```
The image you received is **`25` pixels wide and `6` pixels tall**.
To make sure the image wasn't corrupted during transmission, the Elves would like you to find the layer that contains the **fewest `0` digits**. On that layer, what is the **number of `1` digits multiplied by the number of `2` digits**?
## Part 2
Now you're ready to decode the image. The image is rendered by stacking the layers and aligning the pixels with the same positions in each layer. The digits indicate the color of the corresponding pixel: `0` is black, `1` is white, and `2` is transparent.
The layers are rendered with the first layer in front and the last layer in back. So, if a given position has a transparent pixel in the first and second layers, a black pixel in the third layer, and a white pixel in the fourth layer, the final image would have a **black** pixel at that position.
For example, given an image `2` pixels wide and `2` pixels tall, the image data `0222112222120000` corresponds to the following image layers:
```
Layer 1: 02
22
Layer 2: 11
22
Layer 3: 22
12
Layer 4: 00
00
```
Then, the full image can be found by determining the top visible pixel in each position:
- The top-left pixel is **black** because the top layer is `0`.
- The top-right pixel is **white** because the top layer is `2` (transparent), but the second layer is `1`.
- The bottom-left pixel is **white** because the top two layers are `2`, but the third layer is `1`.
- The bottom-right pixel is **black** because the only visible pixel in that position is `0` (from layer 4).
So, the final image looks like this:
```
01
10
```
**What message is produced after decoding your image?**

42
2019/08/code.py Normal file
View File

@ -0,0 +1,42 @@
""" https://adventofcode.com/2019/day/8 """
def readFile():
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
return f.read()[:-1]
def getLayers(input, width, height):
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):
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):
for layer in layers:
if layer[h][w] != "2": return layer[h][w]
return "2"
def part1(layers):
min, minLayer = None, None
for layer in layers:
cnt = sum([l.count("0") for l in layer])
if min is None or cnt < min:
min, minLayer = cnt, layer
return sum([l.count("1") for l in minLayer]) * sum([l.count("2") for l in minLayer])
def part2(layers):
picture = getPicture(layers)
return f"\n{picture.replace('0', ' ').replace('1', 'X')}"
def test():
assert getLayers("123456789012",3,2) == [["123","456"],["789","012"]]
assert getPicture(getLayers("0222112222120000",2,2)) == "01\n10"
if __name__ == "__main__":
test()
vals = getLayers(readFile(), 25, 6)
print(f"Part 1: {part1(vals)}")
print(f"Part 2: {part2(vals)}")

1
2019/08/input.txt Normal file

File diff suppressed because one or more lines are too long

8
2019/08/solution.txt Normal file
View File

@ -0,0 +1,8 @@
Part 1: 1935
Part 2:
XX XXXX X X X X
X X X X X X X
X XXX X X X X
X X X X X X
X X X X X X X
XX X XXXX XX XXXX

View File

@ -1,5 +1,5 @@
# Advent of Code - 2019 # Advent of Code - 2019
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2019) #### [Direct Link](https://adventofcode.com/2019)
## Intro ## Intro

View File

@ -1 +1,11 @@
# Advent-of-Code # Advent of Code
This repository contains my solutions for [Advent of Code](https://adventofcode.com/).
The folder for each day contains:
- Task of the day
- Input for the given task
- Code to solve the given task
- My solutions to the given input
Please note that all task descriptions on the AoC website could've been changed since they were added to this repository to clarify certain passages.