[2k25] Add day 3
This commit is contained in:
120
Days/Day3.cs
Normal file
120
Days/Day3.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using Spectre.Console;
|
||||
|
||||
namespace AdventOfCode.Days;
|
||||
|
||||
public class Day3 : Day
|
||||
{
|
||||
public override int Number => 3;
|
||||
public override string Name => "Lobby";
|
||||
|
||||
public override void RunPart1(bool display = true)
|
||||
{
|
||||
var totalOutputJoltage = 0L;
|
||||
|
||||
Span<int> bank = stackalloc int[100];
|
||||
|
||||
foreach (var line in Input.EnumerateLines())
|
||||
{
|
||||
// Parse the line into battery values
|
||||
for (var i = 0; i < bank.Length; i++)
|
||||
{
|
||||
bank[i] = line[i] - '0';
|
||||
}
|
||||
|
||||
// The first digit the joltage value will be the "left-most" biggest number we can find
|
||||
var firstDigitMax = bank[0];
|
||||
var firstDigitMaxIndex = 0;
|
||||
|
||||
for (var i = 1; i < bank.Length - 1; i++)
|
||||
{
|
||||
var batteryValue = bank[i];
|
||||
if (batteryValue > firstDigitMax)
|
||||
{
|
||||
firstDigitMax = batteryValue;
|
||||
firstDigitMaxIndex = i;
|
||||
|
||||
// We cannot have a digit bigger than 9
|
||||
if (batteryValue is 9)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now we can find the second digit which will be the biggest digit on the right of the first one
|
||||
var secondDigitMax = bank[firstDigitMaxIndex + 1];
|
||||
for (var i = firstDigitMaxIndex + 2; i < bank.Length; i++)
|
||||
{
|
||||
var batteryValue = bank[i];
|
||||
|
||||
if (batteryValue > secondDigitMax)
|
||||
{
|
||||
secondDigitMax = batteryValue;
|
||||
|
||||
if (batteryValue is 9)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build the joltage value
|
||||
totalOutputJoltage += firstDigitMax * 10 + secondDigitMax;
|
||||
}
|
||||
|
||||
if (display)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[green]The total output joltage is: [yellow]{totalOutputJoltage}[/][/]");
|
||||
}
|
||||
}
|
||||
|
||||
public override void RunPart2(bool display = true)
|
||||
{
|
||||
var totalOutputJoltage = 0L;
|
||||
|
||||
Span<int> bank = stackalloc int[100];
|
||||
|
||||
foreach (var line in Input.EnumerateLines())
|
||||
{
|
||||
// Parse the line into battery values
|
||||
for (var i = 0; i < bank.Length; i++)
|
||||
{
|
||||
bank[i] = line[i] - '0';
|
||||
}
|
||||
|
||||
var maxIndex = -1;
|
||||
for (var digitPosition = 11; digitPosition >= 0; digitPosition--)
|
||||
{
|
||||
// Next max is always on the right of previous one
|
||||
maxIndex++;
|
||||
|
||||
// Find biggest battery value on the right
|
||||
var max = bank[maxIndex];
|
||||
for (var i = maxIndex + 1; i < bank.Length - digitPosition; i++)
|
||||
{
|
||||
var batteryValue = bank[i];
|
||||
|
||||
if (batteryValue > max)
|
||||
{
|
||||
maxIndex = i;
|
||||
max = batteryValue;
|
||||
|
||||
// We cannot have a digit bigger than 9
|
||||
if (batteryValue is 9)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Directly add its value to the total, we don't need to do compound sum
|
||||
totalOutputJoltage += max * (long) Math.Pow(10, digitPosition);
|
||||
}
|
||||
}
|
||||
|
||||
if (display)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[green]The total output joltage is: [yellow]{totalOutputJoltage}[/][/]");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user