From 3f9db77e0ede28dcfaab95c1ac4dea2218c901fc Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Sun, 8 Dec 2024 01:13:19 -0500 Subject: [PATCH] Day 8: Resonant Collinearity --- README.md | 2 +- .../github/ephemient/aoc2024/exe/Day8Bench.kt | 31 ++++++++++++ .../com/github/ephemient/aoc2024/Day8.kt | 47 +++++++++++++++++++ .../com/github/ephemient/aoc2024/Days.kt | 1 + .../com/github/ephemient/aoc2024/Day8Test.kt | 34 ++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day8Bench.kt create mode 100644 kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt create mode 100644 kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day8Test.kt diff --git a/README.md b/README.md index a78cf9a..be56dde 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,4 @@ Development occurs in language-specific directories: |[Day5.hs](hs/src/Day5.hs)|[Day5.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt)|[day5.py](py/aoc2024/day5.py)|[day5.rs](rs/src/day5.rs)| |[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day6.kt)|[day6.py](py/aoc2024/day6.py)|[day6.rs](rs/src/day6.rs)| |[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)|[day7.py](py/aoc2024/day7.py)|[day7.rs](rs/src/day7.rs)| -|[Day8.hs](hs/src/Day8.hs)|||| +|[Day8.hs](hs/src/Day8.hs)|[Day8.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt)||| diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day8Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day8Bench.kt new file mode 100644 index 0000000..7ff0c36 --- /dev/null +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day8Bench.kt @@ -0,0 +1,31 @@ +package com.github.ephemient.aoc2024.exe + +import com.github.ephemient.aoc2024.Day8 +import kotlinx.benchmark.Benchmark +import kotlinx.benchmark.Blackhole +import kotlinx.benchmark.Scope +import kotlinx.benchmark.Setup +import kotlinx.benchmark.State + +@State(Scope.Benchmark) +class Day8Bench { + private lateinit var input: String + + @Setup + fun setup() { + input = getDayInput(8) + } + + @Benchmark + fun part1() = Day8(input).part1() + + @Benchmark + fun part2() = Day8(input).part2() + + @Benchmark + fun solve(bh: Blackhole) { + val day8 = Day8(input) + bh.consume(day8.part1()) + bh.consume(day8.part2()) + } +} diff --git a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt new file mode 100644 index 0000000..ecf81fd --- /dev/null +++ b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt @@ -0,0 +1,47 @@ +package com.github.ephemient.aoc2024 + +class Day8(input: String) { + private val height: Int + private val width: Int + private val antennae: Map> + init { + val lines = input.trimEnd().lines() + height = lines.size + width = lines.maxOfOrNull { it.length } ?: 0 + antennae = buildMap> { + for ((y, line) in lines.withIndex()) { + for ((x, char) in line.withIndex()) { + if (char != '.') getOrPut(char) { mutableListOf() }.add(y to x) + } + } + } + } + + private fun MutableCollection.addIfInRange(y: Int, x: Int): Boolean = + if (y in 0.. = listOf( Day(5, ::Day5, Day5::part1, Day5::part2), Day(6, ::Day6, Day6::part1, Day6::part2), Day(7, ::Day7, Day7::part1, Day7::part2), + Day(8, ::Day8, Day8::part1, Day8::part2), ) data class Day( diff --git a/kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day8Test.kt b/kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day8Test.kt new file mode 100644 index 0000000..fbc82f7 --- /dev/null +++ b/kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day8Test.kt @@ -0,0 +1,34 @@ +package com.github.ephemient.aoc2024 + +import kotlin.test.Test +import kotlin.test.assertEquals + +class Day8Test { + @Test + fun part1() { + assertEquals(14, Day8(example).part1()) + } + + @Test + fun part2() { + assertEquals(34, Day8(example).part2()) + } + + companion object { + private val example = + """ + |............ + |........0... + |.....0...... + |.......0.... + |....0....... + |......A..... + |............ + |............ + |........A... + |.........A.. + |............ + |............ + |""".trimMargin() + } +}