01020304050607080910111213141516171819202122232425

Advent of Code

2019/8

Space Image Format

in C#

by encse

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 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.

Read the full puzzle.

using System;
using System.Collections.Generic;
using System.Linq;

namespace AdventOfCode.Y2019.Day08;

[ProblemName("Space Image Format")]
class Solution : Solver {

    public object PartOne(string input) {
        var zeroMin = int.MaxValue;
        var checksum = 0;
        foreach (var layer in Layers(input)) {
            var zero = layer.Count(item => item == 0);
            var ones = layer.Count(item => item == 1);
            var twos = layer.Count(item => item == 2);

            if (zeroMin > zero) {
                zeroMin = zero;
                checksum = ones * twos;
            }
        }
        return checksum;
    }

    public object PartTwo(string input) {
        var img = new char[6 * 25];
        foreach (var layer in Layers(input).Reverse()) {
            for (var i = 0; i < img.Length; i++) {
                img[i] = layer[i] switch {
                    0 => ' ',
                    1 => '#',
                    _ => img[i]
                };
            }
        }
        return string.Join("", 
            img.Chunk(25).Select(line => string.Join("", line)+"\n")
        ).Ocr();
    }

    int[][] Layers(string input) =>
        input.Select(ch => ch - '0').Chunk(6 * 25).ToArray();
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator