From 1602672470be86eca27d3d37beb81172d9019403 Mon Sep 17 00:00:00 2001 From: encse Date: Sun, 8 Dec 2024 07:42:21 +0100 Subject: [PATCH] 2024/08 writeup --- 2024/Day08/README.md | 10 +++++++++- 2024/Day08/Solution.cs | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/2024/Day08/README.md b/2024/Day08/README.md index bed73a0a..9b219182 100644 --- a/2024/Day08/README.md +++ b/2024/Day08/README.md @@ -1,8 +1,16 @@ ## --- Day 8: Resonant Collinearity --- -You find yourselves on the [roof](/2016/day/25) of a top-secret Easter Bunny installation. +You find yourselves on the _roof_ of a top-secret Easter Bunny installation. While The Historians do their thing, you take a look at the familiar huge antenna. Much to your surprise, it seems to have been reconfigured to emit a signal that makes people 0.1% more likely to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift! Unthinkable! Scanning across the city, you find that there are actually many such antennas. Each antenna is tuned to a specific frequency indicated by a single lowercase letter, uppercase letter, or digit. You create a map (your puzzle input) of these antennas. For example: Read the [full puzzle](https://adventofcode.com/2024/day/8). + +Continuing the steps I started yesterday, I extracted a common function (`GetUniquePositions`) that takes a parameter to generate antinode positions, representing the difference between part one and part two. + +`getAntinodes` returns the antinode positions of srcAntenna on the dstAntenna side. It doesn’t need to be symmetric — i.e., it doesn’t have to return the antinodes on the srcAntenna side — because GetUniquePositions will call it with the parameters swapped as well. This allows us to handle only one direction at a time. + +The generators are fairly straightforward: I simply take steps in the direction determined by the antennas, starting from the destination. Since I represented coordinates using complex numbers again, there’s no need for any special handling on the algebra side, regular + and - operations work. + +The `GetAntinodes` delegate is introduced only for documentation purposes. It looks better to my eyes than an ugly `Func<>` with four type parameters would in `GetUniquePositions`-s signature. diff --git a/2024/Day08/Solution.cs b/2024/Day08/Solution.cs index 48e023d1..56ad08b6 100644 --- a/2024/Day08/Solution.cs +++ b/2024/Day08/Solution.cs @@ -13,7 +13,7 @@ class Solution : Solver { public object PartOne(string input) => GetUniquePositions(input, GetAntinodes1).Count(); public object PartTwo(string input) => GetUniquePositions(input, GetAntinodes2).Count(); - HashSet GetUniquePositions(string input, GetAntinodes antinodePostions) { + HashSet GetUniquePositions(string input, GetAntinodes getAntinodes) { var map = GetMap(input); var antennaLocations = ( @@ -26,18 +26,18 @@ select pos from srcAntenna in antennaLocations from dstAntenna in antennaLocations where srcAntenna != dstAntenna && map[srcAntenna] == map[dstAntenna] - from antinode in antinodePostions(srcAntenna, dstAntenna, map) + from antinode in getAntinodes(srcAntenna, dstAntenna, map) select antinode ).ToHashSet(); } - // returns the antinode positions on the srcAntenna side induced by dstAntenna + // returns the antinode positions of srcAntenna on the dstAntenna side delegate IEnumerable GetAntinodes(Complex srcAntenna, Complex dstAntenna, Map map); // in part 1 we just look at the immediate neighbour IEnumerable GetAntinodes1(Complex srcAntenna, Complex dstAntenna, Map map) { var dir = dstAntenna - srcAntenna; - var antinote = srcAntenna - dir; + var antinote = dstAntenna + dir; if (map.Keys.Contains(antinote)) { yield return antinote; } @@ -46,10 +46,10 @@ IEnumerable GetAntinodes1(Complex srcAntenna, Complex dstAntenna, Map m // in part 2 this becomes a cycle, plus srcAntenna is also a valid position now IEnumerable GetAntinodes2(Complex srcAntenna, Complex dstAntenna, Map map) { var dir = dstAntenna - srcAntenna; - var antinote = srcAntenna; + var antinote = dstAntenna; while (map.Keys.Contains(antinote)) { yield return antinote; - antinote -= dir; + antinote += dir; } }