Implemented Day01 part1 and part2 for 2026

This commit is contained in:
Sebastian Lindemeier committed 2026-12-01 09:22:30 +01:00
1 parent 5257e74029
commit 2f05acf3ca
2 files changed
+28 -75

No files matched your search

+9 -9
View File
@@ -4,7 +4,7 @@ namespace AoC_2026.Tests;
public class Day01Test public class Day01Test
{ {
private readonly IPuzzleSolver<int> _sut; private readonly IPuzzleSolver<long> _sut;
private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private readonly string TestInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Test/day01.txt"; private readonly string TestInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Test/day01.txt";
private readonly string ProdInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Prod/day01.txt"; private readonly string ProdInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Prod/day01.txt";
@@ -14,34 +14,34 @@ public class Day01Test
_sut = new Day01(); _sut = new Day01();
} }
[Fact] [Fact]
public void Part01_Test_equals_3() public void Part01_Test_equals_186276()
{ {
var actual = _sut.SolvePart1(TestInputPath); var actual = _sut.SolvePart1(TestInputPath);
Assert.Equal(3, actual); Assert.Equal(186276, actual);
} }
[Fact] [Fact]
public void Part01_Prod_equals_1097() public void Part01_Prod_equals_112105312()
{ {
var actual = _sut.SolvePart1(ProdInputPath); var actual = _sut.SolvePart1(ProdInputPath);
Assert.Equal(1097, actual); Assert.Equal(112105312, actual);
} }
[Fact] [Fact]
public void Part02_Test_equals_16() public void Part02_Test_equals_193050()
{ {
var actual = _sut.SolvePart2(TestInputPath); var actual = _sut.SolvePart2(TestInputPath);
Assert.Equal(16, actual); Assert.Equal(193050, actual);
} }
[Fact] [Fact]
public void Part02_Prod_equals_7101() public void Part02_Prod_equals_142554384()
{ {
var actual = _sut.SolvePart2(ProdInputPath); var actual = _sut.SolvePart2(ProdInputPath);
Assert.Equal(7101, actual); Assert.Equal(142554384, actual);
} }
} }
+19 -66
View File
@@ -2,83 +2,36 @@
namespace AoC_2026; namespace AoC_2026;
public class Day01 : IPuzzleSolver<int> public class Day01 : IPuzzleSolver<long>
{ {
private string[] ParsePuzzleInput(string path) private int[] ParsePuzzleInput(string path)
{ {
var puzzleInput = File.ReadAllLines(path) var puzzleInput = File.ReadAllLines(path)
.Where(l => !string.IsNullOrWhiteSpace(l)) .Where(l => !string.IsNullOrWhiteSpace(l))
.Select(l => int.Parse(l))
.ToArray(); .ToArray();
return puzzleInput; return puzzleInput;
} }
public int SolvePart1(string pathToPuzzleInput) public long SolvePart1(string pathToPuzzleInput)
{ {
var rotationInstructions = ParsePuzzleInput(pathToPuzzleInput); var input = ParsePuzzleInput(pathToPuzzleInput);
var dialPosition = 50; var result = input
var timesZeroWasHit = 0; .GroupBy(id => id)
foreach (var rotationAmount in rotationInstructions.Select(ParseRotationAmount)) .OrderByDescending(g => g.Count())
{ .Take(3)
dialPosition = (dialPosition + rotationAmount) % 100; .Aggregate(1L, (agg, next) => agg * next.Key);
if (dialPosition == 0) return result;
timesZeroWasHit++;
}
return timesZeroWasHit;
} }
public int SolvePart2(string pathToPuzzleInput) public long SolvePart2(string pathToPuzzleInput)
{ {
var rotationInstructions = ParsePuzzleInput(pathToPuzzleInput); var input = ParsePuzzleInput(pathToPuzzleInput);
var dialPosition = 50; var result = input
var timesZeroWasHit = 0; .GroupBy(id => id)
foreach (var rotationAmount in rotationInstructions.Select(ParseRotationAmount)) .OrderBy(g => g.Count())
{ .Take(3)
var actualRotationAmount = 0; .Aggregate(1L, (agg, next) => agg * next.Key);
if (rotationAmount < 0) return result;
{
(var fullTurns, actualRotationAmount) = DivMod(rotationAmount, -100);
timesZeroWasHit += fullTurns;
if (dialPosition != 0 && dialPosition + actualRotationAmount <= 0)
timesZeroWasHit++;
}
else
{
(var fullTurns, actualRotationAmount) = DivMod(rotationAmount, 100);
timesZeroWasHit += fullTurns;
if (dialPosition + actualRotationAmount >= 100)
timesZeroWasHit++;
}
dialPosition = Mod(dialPosition + actualRotationAmount, 100);
}
return timesZeroWasHit;
}
private (int div, int mod) DivMod(int number, int divisor)
{
return (number / divisor, Mod(number, divisor));
}
private int Mod(int number, int divident)
{
var mod = number % divident;
if (number < 0 && divident > 0)
{
mod = divident + mod;
}
return mod;
}
private static int ParseRotationAmount(string puzzleInput)
{
var direction = puzzleInput[..1];
var rotationAmount = int.Parse(puzzleInput[1..]);
if (direction == "L")
{
rotationAmount *= -1;
}
return rotationAmount;
} }
} }