Skip to content
advent of code 2019

day 08

space image format

approach

the image is one long string of digits, cut into layers of width * height pixels by get_layers. part 1 finds the layer with the fewest 0s and prints its count of 1s times its count of 2s.

part 2 goes pixel by pixel, takes the value from the first layer where it isn't 2 (transparent), and returns the decoded pixels as a flat list. turning that list into rows to read the message is left to you.

the trick

the first non-transparent layer, front to back, decides each pixel.

python
for i in range(w*h):
    for l in layers:
        if l[i]!='2':
            decoded[i]=l[i]
            break