Day 8 was a refreshing break from the Intcode vm and it was exactly what I like out of programming puzzles: simple premise, straightforward, and short implementation time once you’ve devised a valid approach.

As always, you can find my full solutions repo here.


For part one, I worked with a list of lists, with each inner list as a “layer” of data. The two money-maker functions are one to count the number of times a character appears in the layer and one to find the layer with the fewest zeroes.

def count_char(layer, char):
    count = 0
    for digit in layer:
        if digit == char:
            count += 1
    return count

def find_fewest_zeroes(layers):
    fewest_layer = 0
    least_zeroes_ct = maxsize
    for index, layer in enumerate(layers):
        zeroes = count_char(layer, "0")
        if zeroes < least_zeroes_ct:
            least_zeroes_ct = zeroes
            fewest_layer = index
    return fewest_layer

From there, you just have to find the right layer then multiply count_char(layer, "1") times count_char(layer, "2"). Voila.

Starting part two, I realized that it would be much easier to work with the layers in a row/column format, so I made each layer a 2d list. Then, I made a function to find the first non-transparent color for each pixel in a layer.

def find_pixel(layers, row, col):
    for layer in layers:
        pixel = layer[row][col]
        if pixel != "2":
            return pixel
    return 2

Creating the output image is super easy once you have the data in an amenable format.

layers = split_rows(layers)
pixels = []
for i in range(0, 6):
    for j in range(0, 25):
        pixels[i].append(find_pixel(layers, i, j))

Now, for another Intcode puzzle…

As always, you can find my full solutions repo here.