Skip to content

Commit

Permalink
2024 Day08 Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 8, 2024
1 parent a2db72f commit d0e548d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
52 changes: 40 additions & 12 deletions Solutions/2024/Day08.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Reflection.Emit;

namespace AdventOfCode.Solutions._2024;
namespace AdventOfCode.Solutions._2024;

/// <summary>
/// Day 08: Resonant Collinearity
Expand All @@ -13,29 +11,41 @@ public static partial class Day08 {
private static ILookup<char, Point> _antennae = default!;

[Init]
public static void LoadMap(string[] input)
public static void LoadMap(string[] input, Action<string[], bool>? 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<string[], bool>? visualise = null)
{
List<Point> antinodes = [.._antennae
.Select(a => _antennae[a.Key])
.Select(locations => locations.Antinodes())
.SelectMany(antinodes => antinodes)
.Distinct()
.Where(antinode => _map.IsInBounds(antinode))];
List<Point> antinodes = _antennae.GetAntinodes(Antinodes);
_map.VisualiseMap(antinodes, "Final", visualise);

return antinodes.Count;
}

public static int Part2(string[] _, Action<string[], bool>? visualise = null)
{
List<Point> 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<Point> GetAntinodes(this ILookup<char, Point> antennae, Func<IEnumerable<Point>, IEnumerable<Point>> antinodesFunc)
{
return [..antennae
.Select(a => antennae[a.Key])
.Select(antinodesFunc)
.SelectMany(antinodes => antinodes)
.Distinct()
.Where(antinode => _map.IsInBounds(antinode))];
}

private static IEnumerable<Point> Antinodes(this IEnumerable<Point> locations)
{
Expand All @@ -47,6 +57,25 @@ private static IEnumerable<Point> Antinodes(this IEnumerable<Point> locations)
}
}

private static IEnumerable<Point> ResidentHarmonicAntinodes(this IEnumerable<Point> locations, char[,] map)
{
foreach (IEnumerable<Point> 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<Point> antinodes, string title, Action<string[], bool>? visualise)
{
Expand All @@ -62,6 +91,5 @@ public static void VisualiseMap(this char[, ] map, IEnumerable<Point> antinodes,
}
}

private const char EMPTY = '.';
private const char ANTINODE = '#';
}
8 changes: 8 additions & 0 deletions Tests/2024/Tests_08.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[], bool>(Callback)), out int actual);
actual.ShouldBe(expected);
}


private void Callback(string[] lines, bool _)
{
Expand Down

0 comments on commit d0e548d

Please sign in to comment.