Skip to content

Advent of Code – Day 12

Christmas countdown

Photo by Any Lane from Pexels

I’m participating in the Advent of Code 2021. Here’s my solutions for Day 12 – Passage Pathing.

Problem 1

The Puzzles for Day 12 are a pathing problem. We are finding our way through a cave system. The system has a start point, and end point and a series of caves that are either large caves (represented by capital letters) or small caves (represented by lower case letters). The cave system input is in a the format of:

start-A
start-b
A-c
A-b
b-d
A-end
b-end

This cave system represented graphically would be:

    start
    /   
c--A-----b--d
       /
     end

We want to find all paths through the cave that doesn’t visit any particular small cave more than once.

Solution

All my solutions are written in C#. You can find all my solutions in my Git repo.

We approach in steps. First thing we’ll do is to iterate through every octopus and add 1 to its value. Once we’ve increased every position by one, we’ll circle back through and figure out which ones flashed. If it flashes, we’ll add to the level of every octopus in the circle around that one. Then, we’ll see if any more of them flashed. And keep iterating through until no more flashes have occurred.

Console.WriteLine("Advent of Code 2021");
Console.WriteLine("Day 11 - Puzzle 1");
//Solution logic goes here
//Load input array
string[] lines = System.IO.File.ReadAllLines("input.txt");
var flashes = 0;
//Load grid
var octopi = new int[10, 10];
for (int a = 0; a < 10; a++)
{
    for (int b = 0; b < 10; b++)
    {
        octopi[a, b] = Convert.ToInt32((lines[a][b]).ToString());
    }
}
//now run through 100 steps
for (var z = 0; z < 100; z++)
{
    //increase every position first
    for (int a = 0; a < 10; a++)
    {
        for (int b = 0; b < 10; b++)
        {
            octopi[a, b]++;
        }
    }
    //check flashes
    var foundFlashes = true;
    while (foundFlashes)
    {
        foundFlashes = false;
        for (int a = 0; a < 10; a++)
        {
            for (int b = 0; b < 10; b++)
            {
                if (octopi[a, b] > 9)
                {
                    foundFlashes = true;
                    Flash(a, b);
                }
            }
        }
    }
    //now reset the -1 to 0 before the next step
    for (int a = 0; a < 10; a++)
    {
        for (int b = 0; b < 10; b++)
        {
            if (octopi[a, b] == -1)
            {
                octopi[a, b] = 0;
            }
        }
    }
    for (int a = 0; a < 10; a++)
    {
        Console.WriteLine();
        for (int b = 0; b < 10; b++)
        {
            Console.Write(octopi[a, b]);
        }
    }
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine();
}
Console.WriteLine($"Total flashes after 100 steps: {flashes}");
//Stop and wait for enter before exiting
Console.ReadLine();

void Flash(int x, int y)
{
    flashes++;
    octopi[x, y] = -1;  //we set to -1 to prevent multiple flashes in a step
    if (x - 1 >= 0 && y - 1 >= 0 && octopi[x - 1, y - 1] >= 0)
    {
        octopi[x - 1, y - 1]++;
    }
    if (y - 1 >= 0 && octopi[x, y - 1] >= 0)
    {
        octopi[x, y - 1]++;
    }
    if (x + 1 < 10 && y - 1 >= 0 && octopi[x + 1, y - 1] >= 0)
    {
        octopi[x + 1, y - 1]++;
    }
    if (x - 1 >= 0 && octopi[x - 1, y] >= 0)
    {
        octopi[x - 1, y]++;
    }
    if (x + 1 < 10 && octopi[x + 1, y] >= 0)
    {
        octopi[x + 1, y]++;
    }
    if (x - 1 >= 0 && y + 1 < 10 && octopi[x - 1, y + 1] >= 0)
    {
        octopi[x - 1, y + 1]++;
    }
    if (y + 1 < 10 && octopi[x, y + 1] >= 0)
    {
        octopi[x, y + 1]++;
    }
    if (x + 1 < 10 && y + 1 < 10 && octopi[x + 1, y + 1] >= 0)
    {
        octopi[x + 1, y + 1]++;
    }
}

The answer is: 1683

Puzzle 2

For the second half of the problem, we will continue to step until every octopus flashes within a single step.

Solution

To do this, we’ll change the code from puzzle 1 to continue with the steps, checking at the end of each step to see if every octopus flashed.

Console.WriteLine("Advent of Code 2021");
Console.WriteLine("Day 11 - Puzzle 2");
//Solution logic goes here
//Load input array
string[] lines = System.IO.File.ReadAllLines("input.txt");
var flashes = 0;
//Load grid
var octopi = new int[10, 10];
for (int a = 0; a < 10; a++)
{
    for (int b = 0; b < 10; b++)
    {
        octopi[a, b] = Convert.ToInt32((lines[a][b]).ToString());
    }
}
//Now determine what step synchronization occurs on
var step = 0;
var allSynched = false;
while (!allSynched)
{
    step++;
    //increase every position first
    for (int a = 0; a < 10; a++)
    {
        for (int b = 0; b < 10; b++)
        {
            octopi[a, b]++;
        }
    }
    //check flashes
    var foundFlashes = true;
    while (foundFlashes)
    {
        foundFlashes = false;
        for (int a = 0; a < 10; a++)
        {
            for (int b = 0; b < 10; b++)
            {
                if (octopi[a, b] > 9)
                {
                    foundFlashes = true;
                    Flash(a, b);
                }
            }
        }
    }
    //check if all synched
    allSynched = true;
    for (int a = 0; a < 10; a++)
    {
        for (int b = 0; b < 10; b++)
        {
            if (octopi[a, b] > -1)
            {
                allSynched = false;
            }
        }
    }
    if (!allSynched)
    {
        //now reset the -1 to 0 before the next step
        for (int a = 0; a < 10; a++)
        {
            for (int b = 0; b < 10; b++)
            {
                if (octopi[a, b] == -1)
                {
                    octopi[a, b] = 0;
                }
            }
        }
    }
    for (int a = 0; a < 10; a++)
    {
        Console.WriteLine();
        for (int b = 0; b < 10; b++)
        {
            Console.Write(octopi[a, b]);
        }
    }
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine();
}
Console.WriteLine($"Step of synchronization: {step}");
//Stop and wait for enter before exiting
Console.ReadLine();

void Flash(int x, int y)
{
    flashes++;
    octopi[x, y] = -1;  //we set to -1 to prevent multiple flashes in a step
    if (x - 1 >= 0 && y - 1 >= 0 && octopi[x - 1, y - 1] >= 0)
    {
        octopi[x - 1, y - 1]++;
    }
    if (y - 1 >= 0 && octopi[x, y - 1] >= 0)
    {
        octopi[x, y - 1]++;
    }
    if (x + 1 < 10 && y - 1 >= 0 && octopi[x + 1, y - 1] >= 0)
    {
        octopi[x + 1, y - 1]++;
    }
    if (x - 1 >= 0 && octopi[x - 1, y] >= 0)
    {
        octopi[x - 1, y]++;
    }
    if (x + 1 < 10 && octopi[x + 1, y] >= 0)
    {
        octopi[x + 1, y]++;
    }
    if (x - 1 >= 0 && y + 1 < 10 && octopi[x - 1, y + 1] >= 0)
    {
        octopi[x - 1, y + 1]++;
    }
    if (y + 1 < 10 && octopi[x, y + 1] >= 0)
    {
        octopi[x, y + 1]++;
    }
    if (x + 1 < 10 && y + 1 < 10 && octopi[x + 1, y + 1] >= 0)
    {
        octopi[x + 1, y + 1]++;
    }
}

The answer is: 788