Advent of Code 2021 – Day 5

Photo by Any Lane from Pexels

I’m participating in the Advent of Code 2021. Here’s my solutions for Day 5 – Hydrothermal Vents. Going forward I’m combining the posts into a single post per day.

Problem 1

Puzzles for Day 5 is about finding the intersections of lines plotted on a grid. In this case, we have a set of lines defined by the endpoints across a 1000 x 1000 grid. For puzzle 1, we will only look at horizontal and vertical lines and count the points where at least two lines intersect.

Solution

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

We first need to set up our grid and initialize the values to 0. Then, we’ll iterate through each input line and plot it on the grid if it’s horizontal or vertical. If it’s diagonal, we’ll ignore it for now. For each point on each line, we’ll increase the count on that point on the grid by 1. Once all input lines are parsed, we’ll count the points where the value is 2 or greater.

``````Console.WriteLine("Advent of Code 2021");
Console.WriteLine("Day 5 - Puzzle 1");
//Solution logic goes here
//initialize grid
int[,] seaGrid = new int[1000, 1000];
for (int a = 0; a < 1000; a++)
{
for (int b = 0; b < 1000; b++)
{
seaGrid[a, b] = 0;
}
}
//now parse the lines
foreach (var line in lines)
{
//get our endpoints as ints
var points = line.Replace(" ", "").Split("->");
var startPoint = points[0].Split(",");
var x1 = Convert.ToInt32(startPoint[0]);
var y1 = Convert.ToInt32(startPoint[1]);
var endPoint = points[1].Split(",");
var x2 = Convert.ToInt32(endPoint[0]);
var y2 = Convert.ToInt32(endPoint[1]);
if (x1 == x2)
{
//vertical line
if(y1 > y2)
{
var temp = y2;
y2 = y1;
y1 = temp;
}
for (var z = y1; z <= y2; z++)
{
seaGrid[x1, z] = seaGrid[x1, z] + 1;
}
}
else if (y1 == y2)
{
//horizontal line
if(x1 > x2)
{
var temp = x2;
x2 = x1;
x1 = temp;
}
for (var t = x1; t <= x2; t++)
{
seaGrid[t, y1] = seaGrid[t, y1] + 1;
}
}
else
{
//diagonal line

}
}
//now find count where intersection >= 2
var counter = 0;
for (int f = 0; f < 1000; f++)
{
for (int g = 0; g < 1000; g++)
{
if (seaGrid[f, g] >= 2)
{
counter++;
};
}
}
Console.WriteLine(\$"Number of points >= 2:  {counter}");
//Stop and wait for enter before exiting

Problem 2

For the second puzzle, we will be adding in the diagonal lines.

Solution

In this case, it will be easy since all diagonal lines are exactly 45 degrees. So we don’t need to mess with calculating slopes or offsets or anything like that. We’ll just need to determine if it’s top left to bottom right or bottom left to top right and iterate accordingly. I’m only included the additional code in the diagonal block here.

``````        //diagonal line
//determine angle (top left to bottom right or bottom left to top right)
int xPath = 1, yPath = 1;
if (x1 > x2) xPath = -1;
if (y1 > y2) yPath = -1;
//Console.WriteLine(\$"{x1},{y1} -> {x2},{y2}");
//now find all the points
var diff = Math.Abs(x1 - x2);
for(int t=0; t <= diff; t++)
{
var x = x1 + (t * xPath);
var y = y1 + (t * yPath);
seaGrid[x, y] = seaGrid[x, y] + 1;
}``````