Files
AdventOfCode/Days/Day4.cs
2025-12-04 21:35:12 +01:00

156 lines
4.2 KiB
C#

using Spectre.Console;
namespace AdventOfCode.Days;
public class Day4 : Day
{
public override int Number => 4;
public override string Name => "Printing Department";
public const int Length = 136;
public override void RunPart1(bool display = true)
{
var accessibleRollsOfPaper = 0;
var map = ParseMap(Input);
Span<(int i, int j)> adjacentPositions =
[
(-1, -1), (-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0), ( 1, 1)
];
for (var i = 0; i < Length; i++)
{
for (var j = 0; j < Length; j++)
{
// There is no paper roll at position (i, j)
if (!map[i, j])
{
continue;
}
// Check if a paper roll is accessible
var adjacentRolls = 0;
foreach (var adjacentPosition in adjacentPositions)
{
var targetI = i + adjacentPosition.i;
var targetJ = j + adjacentPosition.j;
// Check that the adjacent position is not out of bounds
if (targetI is < 0 or >= Length || targetJ is < 0 or >= Length)
{
continue;
}
if (map[targetI, targetJ])
{
adjacentRolls++;
}
}
if (adjacentRolls < 4)
{
accessibleRollsOfPaper++;
}
}
}
if (display)
{
AnsiConsole.MarkupLine($"[green]Accessible rolls of paper: [yellow]{accessibleRollsOfPaper}[/][/]");
}
}
public override void RunPart2(bool display = true)
{
var accessibleRollsOfPaper = 0;
var map = ParseMap(Input);
Span<(int i, int j)> adjacentPositions =
[
(-1, -1), (-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0), ( 1, 1)
];
int removedRollsOfPaper;
// Loop until we cannot remove any roll anymore
do
{
removedRollsOfPaper = 0;
for (var i = 0; i < Length; i++)
{
for (var j = 0; j < Length; j++)
{
// There is no paper roll at position (i, j)
if (!map[i, j])
{
continue;
}
// Check if a paper roll is accessible
var adjacentRolls = 0;
foreach (var adjacentPosition in adjacentPositions)
{
var targetI = i + adjacentPosition.i;
var targetJ = j + adjacentPosition.j;
// Check that the adjacent position is not out of bounds
if (targetI is < 0 or >= Length || targetJ is < 0 or >= Length)
{
continue;
}
if (map[targetI, targetJ])
{
adjacentRolls++;
}
}
if (adjacentRolls < 4)
{
removedRollsOfPaper++;
// Remove roll of paper
map[i, j] = false;
}
}
}
accessibleRollsOfPaper += removedRollsOfPaper;
} while (removedRollsOfPaper > 0);
if (display)
{
AnsiConsole.MarkupLine($"[green]Accessible rolls of paper: [yellow]{accessibleRollsOfPaper}[/][/]");
}
}
private static bool[,] ParseMap(string input)
{
// Map is a square
var map = new bool[Length, Length];
var i = 0;
foreach (var line in input.EnumerateLines())
{
var j = 0;
foreach (var value in line)
{
map[i, j] = value is '@';
j++;
}
i++;
}
return map;
}
}