diff --git a/Solutions/2024/Day08.cs b/Solutions/2024/Day08.cs index a6f58a1..74b3394 100644 --- a/Solutions/2024/Day08.cs +++ b/Solutions/2024/Day08.cs @@ -1,6 +1,4 @@ -using System.Reflection.Emit; - -namespace AdventOfCode.Solutions._2024; +namespace AdventOfCode.Solutions._2024; /// /// Day 08: Resonant Collinearity @@ -13,29 +11,41 @@ public static partial class Day08 { private static ILookup _antennae = default!; [Init] - public static void LoadMap(string[] input) + public static void LoadMap(string[] input, Action? visualise = null) { _map = input.To2dArray(); _antennae = _map .ForEachCell() .Where(c => char.IsAsciiLetterOrDigit(c)) .ToLookup(a => a.Value, a => a.Index); + _map.VisualiseMap([], "Initial", visualise); } public static int Part1(string[] _, Action? visualise = null) { - List antinodes = [.._antennae - .Select(a => _antennae[a.Key]) - .Select(locations => locations.Antinodes()) - .SelectMany(antinodes => antinodes) - .Distinct() - .Where(antinode => _map.IsInBounds(antinode))]; + List antinodes = _antennae.GetAntinodes(Antinodes); + _map.VisualiseMap(antinodes, "Final", visualise); + + return antinodes.Count; + } + public static int Part2(string[] _, Action? visualise = null) + { + List antinodes = _antennae.GetAntinodes(a => a.ResidentHarmonicAntinodes(_map)); _map.VisualiseMap(antinodes, "Final", visualise); + return antinodes.Count; } - public static string Part2(string[] _) => NO_SOLUTION_WRITTEN_MESSAGE; + private static List GetAntinodes(this ILookup antennae, Func, IEnumerable> antinodesFunc) + { + return [..antennae + .Select(a => antennae[a.Key]) + .Select(antinodesFunc) + .SelectMany(antinodes => antinodes) + .Distinct() + .Where(antinode => _map.IsInBounds(antinode))]; + } private static IEnumerable Antinodes(this IEnumerable locations) { @@ -47,6 +57,25 @@ private static IEnumerable Antinodes(this IEnumerable locations) } } + private static IEnumerable ResidentHarmonicAntinodes(this IEnumerable locations, char[,] map) + { + foreach (IEnumerable item in locations.Combinations(2)) { + Point a1 = item.First(); + Point a2 = item.Last(); + int i = 0; + while (map.IsInBounds(((a1 - a2) * i) + a1)) { + yield return ((a1 - a2) * i) + a1; + i++; + } + + i = 0; + while (map.IsInBounds(((a2 - a1) * i) + a2)) { + yield return ((a2 - a1) * i) + a2; + i++; + } + } + } + public static void VisualiseMap(this char[, ] map, IEnumerable antinodes, string title, Action? visualise) { @@ -62,6 +91,5 @@ public static void VisualiseMap(this char[, ] map, IEnumerable antinodes, } } - private const char EMPTY = '.'; private const char ANTINODE = '#'; } diff --git a/Tests/2024/Tests_08.cs b/Tests/2024/Tests_08.cs index f02cea8..e346b64 100644 --- a/Tests/2024/Tests_08.cs +++ b/Tests/2024/Tests_08.cs @@ -27,6 +27,14 @@ public void Part1(string input, int expected) actual.ShouldBe(expected); } + [Theory] + [InlineData(TEST_DATA, 34)] + public void Part2(string input, int expected) + { + _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input, new Action(Callback)), out int actual); + actual.ShouldBe(expected); + } + private void Callback(string[] lines, bool _) {