Skip to content

Commit 9f06d41

Browse files
committed
Add solution for Y2024D09.
1 parent 71a9aa8 commit 9f06d41

File tree

6 files changed

+112
-11
lines changed

6 files changed

+112
-11
lines changed

Diff for: README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ _"Anything that's worth doing, is worth overdoing."_
1515
Table of contents to jump straight to the problem you're looking for.
1616

1717
<details open>
18-
<summary>2024 (14x⭐)</summary>
19-
20-
| Day | Title | Stars |
21-
|:---:|:--------------------------------------------------------|:-----:|
22-
| 01 | [Historian Hysteria](solutions/aockt/y2024/Y2024D01.kt) | ⭐⭐ |
23-
| 02 | [Red-Nosed Reports](solutions/aockt/y2024/Y2024D02.kt) | ⭐⭐ |
24-
| 03 | [Mull It Over](solutions/aockt/y2024/Y2024D03.kt) | ⭐⭐ |
25-
| 04 | [Ceres Search](solutions/aockt/y2024/Y2024D04.kt) | ⭐⭐ |
26-
| 05 | [Print Queue](solutions/aockt/y2024/Y2024D05.kt) | ⭐⭐ |
27-
| 06 | [Guard Gallivant](solutions/aockt/y2024/Y2024D06.kt) | ⭐⭐ |
28-
| 07 | [Bridge Repair](solutions/aockt/y2024/Y2024D07.kt) | ⭐⭐ |
18+
<summary>2024 (16x⭐)</summary>
19+
20+
| Day | Title | Stars |
21+
|:---:|:-----------------------------------------------------------|:-----:|
22+
| 01 | [Historian Hysteria](solutions/aockt/y2024/Y2024D01.kt) | ⭐⭐ |
23+
| 02 | [Red-Nosed Reports](solutions/aockt/y2024/Y2024D02.kt) | ⭐⭐ |
24+
| 03 | [Mull It Over](solutions/aockt/y2024/Y2024D03.kt) | ⭐⭐ |
25+
| 04 | [Ceres Search](solutions/aockt/y2024/Y2024D04.kt) | ⭐⭐ |
26+
| 05 | [Print Queue](solutions/aockt/y2024/Y2024D05.kt) | ⭐⭐ |
27+
| 06 | [Guard Gallivant](solutions/aockt/y2024/Y2024D06.kt) | ⭐⭐ |
28+
| 07 | [Bridge Repair](solutions/aockt/y2024/Y2024D07.kt) | ⭐⭐ |
29+
| 08 | [Resonant Collinearity](solutions/aockt/y2024/Y2024D08.kt) | ⭐⭐ |
2930

3031
</details>
3132

Diff for: inputs/aockt/y2024/d08/input.txt

2.51 KB
Binary file not shown.

Diff for: inputs/aockt/y2024/d08/solution_part1.txt

26 Bytes
Binary file not shown.

Diff for: inputs/aockt/y2024/d08/solution_part2.txt

27 Bytes
Binary file not shown.

Diff for: solutions/aockt/y2024/Y2024D08.kt

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package aockt.y2024
2+
3+
import aockt.util.math.distinctPairs
4+
import aockt.util.parse
5+
import aockt.util.spacial.*
6+
import io.github.jadarma.aockt.core.Solution
7+
8+
object Y2024D08 : Solution {
9+
10+
/**
11+
* The map of antennas on the Easter Bunny roof.
12+
* @property area The bounds to check nodes in.
13+
* @property antennas All the antenna positions, group by the frequencies they emit.
14+
*/
15+
private class AntennaMap(val area: Area, val antennas: Map<Char, Set<Point>>) {
16+
17+
/**
18+
* Return all the unique points within the [area] that form at least one frequency anti-node.
19+
* If [repeating] is enabled, an anti-node will spawn at every collinear interval between two matching
20+
* antennas _(including themselves)_.
21+
*/
22+
fun antiNodes(repeating: Boolean = false): Set<Point> = buildSet {
23+
antennas.values
24+
.asSequence()
25+
.flatMap(Set<Point>::distinctPairs)
26+
.forEach { (a, b) ->
27+
val dx = a.x - b.x
28+
val dy = a.y - b.y
29+
var step = if (repeating) 0 else 1
30+
while (repeating || step == 1) {
31+
val p1 = Point(x = a.x + dx * step, y = a.y + dy * step).takeIf { it in area }?.let(::add)
32+
val p2 = Point(x = b.x - dx * step, y = b.y - dy * step).takeIf { it in area }?.let(::add)
33+
if (p1 == null && p2 == null) break
34+
step++
35+
}
36+
}
37+
}
38+
}
39+
40+
/** Parse the [input] and return the [AntennaMap]. */
41+
private fun parseInput(input: String): AntennaMap = parse {
42+
with(Grid(input)) {
43+
AntennaMap(
44+
area = Area(width, height),
45+
antennas = points()
46+
.filter { it.value != '.' }
47+
.groupBy({ it.value }, { it.position })
48+
.mapValues { (_, points) -> points.toSet() },
49+
)
50+
}
51+
}
52+
53+
override fun partOne(input: String): Int = parseInput(input).antiNodes(repeating = false).count()
54+
override fun partTwo(input: String): Int = parseInput(input).antiNodes(repeating = true).count()
55+
}

Diff for: tests/aockt/y2024/Y2024D08Test.kt

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package aockt.y2024
2+
3+
import io.github.jadarma.aockt.test.AdventDay
4+
import io.github.jadarma.aockt.test.AdventSpec
5+
6+
@AdventDay(2024, 8, "Resonant Collinearity")
7+
class Y2024D08Test : AdventSpec<Y2024D08>({
8+
9+
val exampleInput = """
10+
............
11+
........0...
12+
.....0......
13+
.......0....
14+
....0.......
15+
......A.....
16+
............
17+
............
18+
........A...
19+
.........A..
20+
............
21+
............
22+
""".trimIndent()
23+
24+
val exampleT = """
25+
T.........
26+
...T......
27+
.T........
28+
..........
29+
..........
30+
..........
31+
..........
32+
..........
33+
..........
34+
..........
35+
""".trimIndent()
36+
37+
partOne {
38+
exampleInput shouldOutput 14
39+
}
40+
41+
partTwo {
42+
exampleInput shouldOutput 34
43+
exampleT shouldOutput 9
44+
}
45+
})

0 commit comments

Comments
 (0)