Files
AdventOfCode/Days/Day1.cs
2025-12-01 22:14:56 +01:00

87 lines
2.1 KiB
C#

using Spectre.Console;
namespace AdventOfCode.Days;
public class Day1 : Day
{
public override int Number => 1;
public override string Name => "Secret Entrance";
public override void RunPart1(bool display = true)
{
var password = 0;
var dialPosition = 50;
foreach (var line in Input.EnumerateLines())
{
var direction = line[0];
var offset = int.Parse(line[1..]);
var sign = direction is 'L'
? -1
: +1;
dialPosition = MathMod(dialPosition + (offset * sign), 100);
if (dialPosition is 0)
{
password++;
}
}
if (display)
{
AnsiConsole.MarkupLine($"[green]Safe password is: [yellow]{password}[/][/]");
}
}
public override void RunPart2(bool display = true)
{
var password = 0;
var dialPosition = 50;
foreach (var line in Input.EnumerateLines())
{
var direction = line[0];
var offset = int.Parse(line[1..]);
var originalSign = Math.Sign(dialPosition);
var sign = direction is 'L'
? -1
: +1;
dialPosition = dialPosition + (offset * sign);
int clickCount;
if (dialPosition is 0)
{
clickCount = 1;
}
else
{
clickCount = Math.Abs(dialPosition) / 100; // Number of times the dial passed by 0
// If we did a loop around it counts as a "click" too
if (originalSign != 0 && originalSign != Math.Sign(dialPosition))
{
clickCount++;
}
}
dialPosition = MathMod(dialPosition, 100);
password += clickCount;
}
if (display)
{
AnsiConsole.MarkupLine($"[green]Safe password is: [yellow]{password}[/][/]");
}
}
private static int MathMod(int left, int right)
{
return (Math.Abs(left * right) + left) % right;
}
}