Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions docs/day8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
url: "https://adventofcode.com/2024/day/8"
---

# Day 8: Resonant Collinearity

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:

```txt
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
```

The signal only applies its nefarious effect at specific antinodes based on the resonant frequencies of the antennas. In particular, an antinode occurs at any point that is perfectly in line with two antennas of the same frequency - but only when one of the antennas is twice as far away as the other. This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.

So, for these two antennas with frequency `a`, they create the two antinodes marked with `#`:

```txt
..........
...#......
..........
....a.....
..........
.....a....
..........
......#...
..........
..........
```

Adding a third antenna with the same frequency creates several more antinodes. It would ideally add four antinodes, but two are off the right side of the map, so instead it adds only two:

```txt
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......#...
..........
..........
```

Antennas with different frequencies don't create antinodes; `A` and `a` count as different frequencies. However, antinodes can occur at locations that contain antennas. In this diagram, the lone antenna with frequency capital `A` creates no antinodes but has a lowercase-`a`-frequency antinode at its location:

```txt
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......A...
..........
..........
```

The first example has antennas with two different frequencies, so the antinodes they create look like this, plus an antinode overlapping the topmost `A`-frequency antenna:

```txt
......#....#
...#....0...
....#0....#.
..#....0....
....0....#..
.#....A.....
...#........
#......#....
........A...
.........A..
..........#.
..........#.
```

Because the topmost `A`-frequency antenna overlaps with a `0`-frequency antinode, there are `14` total unique locations that contain an antinode within the bounds of the map.

Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode?
65 changes: 65 additions & 0 deletions src/day8/day8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package day8

import (
"strconv"
"strings"
)

type point struct {
x, y int
}

func (p point) toString() string {
return strings.Join([]string{strconv.Itoa(p.x), strconv.Itoa(p.y)}, ",")
}

func (p point) onMap() bool {
return p.x >= 0 && p.x < xMax && p.y >= 0 && p.y < yMax
}

var xMax, yMax = 0, 0

func Solve(input string) uint {
return solve(parseInput(input))
}

func parseInput(input string) map[rune][]point {
xMax, yMax = 0, len(strings.Split(input, "\n"))
result := make(map[rune][]point)
for j, line := range strings.Split(input, "\n") {
if xMax == 0 {
xMax = len(line)
}
for i, chr := range line {
if chr == '.' {
continue
}

pt := point{x: i, y: j}
result[chr] = append(result[chr], pt)
}
}
return result
}

func solve(input map[rune][]point) uint {
antinodes := make(map[string]interface{})
for _, locations := range input {
for i := 0; i < len(locations)-1; i++ {
for j := i + 1; j < len(locations); j++ {
var dx int = locations[i].x - locations[j].x
var dy int = locations[i].y - locations[j].y
antiNodeNearI := point{x: locations[i].x + dx, y: locations[i].y + dy}
antiNodeNearJ := point{x: locations[j].x - dx, y: locations[j].y - dy}

if antiNodeNearI.onMap() {
antinodes[antiNodeNearI.toString()] = true
}
if antiNodeNearJ.onMap() {
antinodes[antiNodeNearJ.toString()] = true
}
}
}
}
return uint(len(antinodes))
}
24 changes: 24 additions & 0 deletions src/day8/day8_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package day8

import (
"testing"
)

func TestSample(t *testing.T) {
input := `............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............`
result := Solve(input)
if result != 14 {
t.Errorf("Calculated solution was not expected")
}
}
Loading