diff --git a/AoC_2026.Tests/Day02Test.cs b/AoC_2026.Tests/Day02Test.cs new file mode 100644 index 0000000..3a7a51e --- /dev/null +++ b/AoC_2026.Tests/Day02Test.cs @@ -0,0 +1,47 @@ +using AdvenOfCode.Contracts; + +namespace AoC_2026.Tests; + +public class Day02Test +{ + private readonly IPuzzleSolver _sut; + private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + private readonly string TestInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Test/day02.txt"; + private readonly string ProdInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Prod/day02.txt"; + + public Day02Test() + { + _sut = new Day02(); + } + [Fact] + public void Part01_Test_equals_16() + { + var actual = _sut.SolvePart1(TestInputPath); + + Assert.Equal(16, actual); + } + + [Fact] + public void Part01_Prod_equals_3415() + { + var actual = _sut.SolvePart1(ProdInputPath); + + Assert.Equal(3415, actual); + } + + [Fact] + public void Part02_Test_equals_32() + { + var actual = _sut.SolvePart2(TestInputPath); + + Assert.Equal(32, actual); + } + + [Fact] + public void Part02_Prod_equals_5467() + { + var actual = _sut.SolvePart2(ProdInputPath); + + Assert.Equal(5467, actual); + } +} \ No newline at end of file diff --git a/AoC_2026/Day02.cs b/AoC_2026/Day02.cs new file mode 100644 index 0000000..c0d50f1 --- /dev/null +++ b/AoC_2026/Day02.cs @@ -0,0 +1,68 @@ +using AdvenOfCode.Contracts; +using AdventOfCode.Extensions; + +namespace AoC_2026; + +public class Day02 : IPuzzleSolver +{ + private static Dictionary _waypointIndex = new Dictionary() + { + ['r'] = 0, + ['o'] = 1, + ['y'] = 2, + ['g'] = 3, + ['b'] = 4, + ['v'] = 5 + }; + + private string ParsePuzzleInput(string path) + { + var puzzleInput = File.ReadAllText(path); + return puzzleInput; + } + + public long SolvePart1(string pathToPuzzleInput) + { + var program = ParsePuzzleInput(pathToPuzzleInput); + var distanceTraveled = GetTravelDistance('r', program); + return distanceTraveled; + } + + private static int GetTravelDistance(char waypoint, string program) + { + var distance = 0; + foreach (var nextWaypoint in program) + { + distance += GetDistance(waypoint, nextWaypoint); + waypoint = nextWaypoint; + } + return distance; + } + + private static int GetDistance(char current, char next) + { + var dist = _waypointIndex[next] - _waypointIndex[current]; + return dist > 0 ? dist : dist + _waypointIndex.Count; + } + + public long SolvePart2(string pathToPuzzleInput) + { + var program = ParsePuzzleInput(pathToPuzzleInput); + var distanceTraveled = GetLongestTravelDistance('r', [..program]); + return distanceTraveled; + } + + private static int GetLongestTravelDistance(char waypoint, char[] program) + { + var distance = 0; + while (program.Length > 0) + { + var currentWaypoint = waypoint; + program = [.. program.OrderByDescending(c => GetDistance(currentWaypoint, c))]; + distance += GetDistance(currentWaypoint, program[0]); + waypoint = program[0]; + program = program[1..]; + } + return distance; + } +} \ No newline at end of file