[2k25] Add day 1
This commit is contained in:
77
Days/Day1.cs
77
Days/Day1.cs
@@ -5,60 +5,83 @@ namespace AdventOfCode.Days;
|
||||
public class Day1 : Day
|
||||
{
|
||||
public override int Number => 1;
|
||||
public override string Name => "Historian Hysteria";
|
||||
public override string Name => "Secret Entrance";
|
||||
|
||||
public override void RunPart1(bool display = true)
|
||||
{
|
||||
List<int> leftNumbers = [];
|
||||
List<int> rightNumbers = [];
|
||||
var password = 0;
|
||||
var dialPosition = 50;
|
||||
|
||||
foreach (var line in Input.AsSpan().EnumerateLines())
|
||||
foreach (var line in Input.EnumerateLines())
|
||||
{
|
||||
var separatorIndex = line.IndexOf(' ');
|
||||
var direction = line[0];
|
||||
var offset = int.Parse(line[1..]);
|
||||
|
||||
leftNumbers.Add(int.Parse(line[..separatorIndex]));
|
||||
rightNumbers.Add(int.Parse(line[(separatorIndex + 1)..]));
|
||||
var sign = direction is 'L'
|
||||
? -1
|
||||
: +1;
|
||||
|
||||
dialPosition = MathMod(dialPosition + (offset * sign), 100);
|
||||
|
||||
if (dialPosition is 0)
|
||||
{
|
||||
password++;
|
||||
}
|
||||
}
|
||||
|
||||
leftNumbers.Sort();
|
||||
rightNumbers.Sort();
|
||||
|
||||
var totalDistance = leftNumbers.Zip(rightNumbers, (a, b) => Math.Abs(a - b)).Sum();
|
||||
|
||||
if (display)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[green]Total distance is: [yellow]{totalDistance}[/][/]");
|
||||
AnsiConsole.MarkupLine($"[green]Safe password is: [yellow]{password}[/][/]");
|
||||
}
|
||||
}
|
||||
|
||||
public override void RunPart2(bool display = true)
|
||||
{
|
||||
List<int> leftNumbers = [];
|
||||
Dictionary<int, int> rightNumbersCount = [];
|
||||
var similarityScore = 0;
|
||||
var password = 0;
|
||||
var dialPosition = 50;
|
||||
|
||||
foreach (var line in Input.AsSpan().EnumerateLines())
|
||||
foreach (var line in Input.EnumerateLines())
|
||||
{
|
||||
var separatorIndex = line.IndexOf(' ');
|
||||
var direction = line[0];
|
||||
var offset = int.Parse(line[1..]);
|
||||
|
||||
leftNumbers.Add(int.Parse(line[..separatorIndex]));
|
||||
var originalSign = Math.Sign(dialPosition);
|
||||
|
||||
var rightNumber = int.Parse(line[(separatorIndex + 1)..]);
|
||||
var sign = direction is 'L'
|
||||
? -1
|
||||
: +1;
|
||||
|
||||
if (!rightNumbersCount.TryAdd(rightNumber, 1))
|
||||
dialPosition = dialPosition + (offset * sign);
|
||||
|
||||
int clickCount;
|
||||
if (dialPosition is 0)
|
||||
{
|
||||
rightNumbersCount[rightNumber]++;
|
||||
clickCount = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
clickCount = Math.Abs(dialPosition) / 100; // Number of times the dial passed by 0
|
||||
|
||||
foreach (var leftNumber in leftNumbers)
|
||||
{
|
||||
similarityScore += leftNumber * rightNumbersCount.GetValueOrDefault(leftNumber);
|
||||
// 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]Similarity score is: [yellow]{similarityScore}[/][/]");
|
||||
AnsiConsole.MarkupLine($"[green]Safe password is: [yellow]{password}[/][/]");
|
||||
}
|
||||
}
|
||||
|
||||
private static int MathMod(int left, int right)
|
||||
{
|
||||
return (Math.Abs(left * right) + left) % right;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user