Skip to content

Commit

Permalink
2024 Day08 refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 8, 2024
1 parent d0e548d commit e2b6aa5
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions Solutions/2024/Day08.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,78 +18,80 @@ public static void LoadMap(string[] input, Action<string[], bool>? visualise = n
.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.GetAntinodes(Antinodes);
List<Point> antinodes = _antennae.GetAntinodes(a => a.Antinodes(_map, start: 1));
_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));
List<Point> antinodes = _antennae.GetAntinodes(a => a.Antinodes(_map, start: 0));
_map.VisualiseMap(antinodes, "Final", visualise);

return antinodes.Count;
}

private static List<Point> GetAntinodes(this ILookup<char, Point> antennae, Func<IEnumerable<Point>, IEnumerable<Point>> antinodesFunc)
{
return [..antennae
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)
private static IEnumerable<Point> Antinodes(this IEnumerable<Point> locations, char[,] map, int start)
{
foreach (IEnumerable<Point> item in locations.Combinations(2)) {
Point a1 = item.First();
Point a2 = item.Last();
yield return a1 - a2 + a1;
yield return a2 - a1 + a2;
foreach (IEnumerable<Point> pair in locations.Combinations(2)) {
Point antenna1 = pair.First();
Point antenna2 = pair.Last();

for (int n = start; ; n++) {
bool allOutOfBounds = true;

foreach (Point antinode in AntinodesN(antenna1, antenna2, n)) {
if (map.IsInBounds(antinode)) {
yield return antinode;
allOutOfBounds = false;
}
}

if (allOutOfBounds || start == 1) {
break;
}
}
}
}

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++;
}
private static IEnumerable<Point> AntinodesN(Point ant1, Point ant2, int n = 1)
=> [((ant1 - ant2) * n) + ant1, ((ant2 - ant1) * n) + ant2];



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)
{
char[,] copy = (char[,])map.Clone();
const char ANTINODE = '#';

char[,] vMap = (char[,])map.Clone();

foreach (Point antinode in antinodes) {
copy[antinode.X, antinode.Y] = ANTINODE;
vMap[antinode.X, antinode.Y] = ANTINODE;
}

if (visualise is not null) {
string[] output = ["", title, .. copy.PrintAsStringArray(0)];
string[] output = ["", title, .. vMap.AsStrings()];
_ = Task.Run(() => visualise?.Invoke(output, false));
}
}

private const char ANTINODE = '#';
}

0 comments on commit e2b6aa5

Please sign in to comment.