Skip to content

Commit

Permalink
2024/08 writeup
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 8, 2024
1 parent 66d6b69 commit 1602672
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
10 changes: 9 additions & 1 deletion 2024/Day08/README.md
Original file line number Diff line number Diff line change
@@ -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 <em>huge antenna</em>. 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 <em>frequency</em> 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.
12 changes: 6 additions & 6 deletions 2024/Day08/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Complex> GetUniquePositions(string input, GetAntinodes antinodePostions) {
HashSet<Complex> GetUniquePositions(string input, GetAntinodes getAntinodes) {
var map = GetMap(input);

var antennaLocations = (
Expand All @@ -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<Complex> GetAntinodes(Complex srcAntenna, Complex dstAntenna, Map map);

// in part 1 we just look at the immediate neighbour
IEnumerable<Complex> 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;
}
Expand All @@ -46,10 +46,10 @@ IEnumerable<Complex> GetAntinodes1(Complex srcAntenna, Complex dstAntenna, Map m
// in part 2 this becomes a cycle, plus srcAntenna is also a valid position now
IEnumerable<Complex> 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;
}
}

Expand Down

0 comments on commit 1602672

Please sign in to comment.