## Advent of Code 2021 - Day 11

Saturday, Dec 11, 2021| Tags: Advent of Code 2021

Photo by Any Lane from Pexels

I’m participating in the Advent of Code 2021. Here’s my solutions for Day 11 - Dumbo Octopus.

## Problem 1

For Puzzles for Day 11, we want to navigate through a cave of octopi by predicting when their bioluminescent energy will flash so we don’t disturb them. The octopi are arranged in a 10x10 grid, with each octopus represented by a power level from 0 to 9.

Each step, the power level of an octopus increases by 1. If it’s at 9, it flashes. Each flash increases the power level of every octopus around it, even diagonally.

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

## INTERESTED ?

I am available for part time consulting work. If you are looking for someone to help with Power Automate, SQL Server, Azure or .NET work, feel free to contact me.