Add day 1
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -13,8 +14,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Inputs\Day1.txt" />
|
||||
<EmbeddedResource Include="Inputs\Day1.txt" />
|
||||
<None Remove="Inputs\*" />
|
||||
<EmbeddedResource Include="Inputs\*" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
90
Days/Day1.cs
90
Days/Day1.cs
@@ -5,96 +5,60 @@ namespace AdventOfCode.Days;
|
||||
public class Day1 : Day
|
||||
{
|
||||
public override int Number => 1;
|
||||
public override string Name => "Trebuchet?!";
|
||||
|
||||
private static readonly (string, int)[] WrittenDigits =
|
||||
[
|
||||
("one" , 1),
|
||||
("two" , 2),
|
||||
("three", 3),
|
||||
("four" , 4),
|
||||
("five" , 5),
|
||||
("six" , 6),
|
||||
("seven", 7),
|
||||
("eight", 8),
|
||||
("nine" , 9)
|
||||
];
|
||||
public override string Name => "Historian Hysteria";
|
||||
|
||||
public override void RunPart1(bool display = true)
|
||||
{
|
||||
var calibrationSum = 0;
|
||||
List<int> leftNumbers = [];
|
||||
List<int> rightNumbers = [];
|
||||
|
||||
foreach (var line in Input.AsSpan().EnumerateLines())
|
||||
{
|
||||
var firstDigitIndex = line.IndexOfAnyInRange('0', '9');
|
||||
var lastDigitIndex = line.LastIndexOfAnyInRange('0', '9');
|
||||
var separatorIndex = line.IndexOf(' ');
|
||||
|
||||
var firstDigit = line[firstDigitIndex] - '0';
|
||||
var lastDigit = line[lastDigitIndex] - '0';
|
||||
|
||||
calibrationSum += firstDigit * 10 + lastDigit;
|
||||
leftNumbers.Add(int.Parse(line[..separatorIndex]));
|
||||
rightNumbers.Add(int.Parse(line[(separatorIndex + 1)..]));
|
||||
}
|
||||
|
||||
leftNumbers.Sort();
|
||||
rightNumbers.Sort();
|
||||
|
||||
var totalDistance = leftNumbers.Zip(rightNumbers, (a, b) => Math.Abs(a - b)).Sum();
|
||||
|
||||
if (display)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[green]Sum of calibration values is: [yellow]{calibrationSum}[/][/]");
|
||||
AnsiConsole.MarkupLine($"[green]Total distance is: [yellow]{totalDistance}[/][/]");
|
||||
}
|
||||
}
|
||||
|
||||
public override void RunPart2(bool display = true)
|
||||
{
|
||||
var calibrationSum = 0;
|
||||
List<int> leftNumbers = [];
|
||||
Dictionary<int, int> rightNumbersCount = [];
|
||||
var similarityScore = 0;
|
||||
|
||||
foreach (var line in Input.AsSpan().EnumerateLines())
|
||||
{
|
||||
var minFirstDigitIndex = line.IndexOfAnyInRange('0', '9');
|
||||
var maxLastDigitIndex = line.LastIndexOfAnyInRange('0', '9');
|
||||
var separatorIndex = line.IndexOf(' ');
|
||||
|
||||
var firstDigitValue = 0;
|
||||
if (minFirstDigitIndex == -1)
|
||||
leftNumbers.Add(int.Parse(line[..separatorIndex]));
|
||||
|
||||
var rightNumber = int.Parse(line[(separatorIndex + 1)..]);
|
||||
|
||||
if (!rightNumbersCount.TryAdd(rightNumber, 1))
|
||||
{
|
||||
minFirstDigitIndex = int.MaxValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
firstDigitValue = line[minFirstDigitIndex] - '0';
|
||||
rightNumbersCount[rightNumber]++;
|
||||
}
|
||||
}
|
||||
|
||||
var lastDigitValue = 0;
|
||||
if (maxLastDigitIndex == -1)
|
||||
{
|
||||
maxLastDigitIndex = int.MinValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
lastDigitValue = line[maxLastDigitIndex] - '0';
|
||||
}
|
||||
|
||||
foreach (var (writtenDigit, value) in WrittenDigits)
|
||||
{
|
||||
var firstWrittenDigitIndex = line.IndexOf(writtenDigit);
|
||||
|
||||
if (firstWrittenDigitIndex != -1 && firstWrittenDigitIndex < minFirstDigitIndex)
|
||||
{
|
||||
minFirstDigitIndex = firstWrittenDigitIndex;
|
||||
firstDigitValue = value;
|
||||
}
|
||||
|
||||
var lastWrittenDigitIndex = line.LastIndexOf(writtenDigit);
|
||||
|
||||
if (lastWrittenDigitIndex != -1 && lastWrittenDigitIndex > maxLastDigitIndex)
|
||||
{
|
||||
maxLastDigitIndex = lastWrittenDigitIndex;
|
||||
lastDigitValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
calibrationSum += 10 * firstDigitValue + lastDigitValue;
|
||||
foreach (var leftNumber in leftNumbers)
|
||||
{
|
||||
similarityScore += leftNumber * rightNumbersCount.GetValueOrDefault(leftNumber);
|
||||
}
|
||||
|
||||
if (display)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[green]Sum of calibration values is: [yellow]{calibrationSum}[/][/]");
|
||||
AnsiConsole.MarkupLine($"[green]Similarity score is: [yellow]{similarityScore}[/][/]");
|
||||
}
|
||||
}
|
||||
}
|
||||
2000
Inputs/Day1.txt
2000
Inputs/Day1.txt
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user