Skip to content

Commit

Permalink
Merge pull request #64 from ephemient/kt/day8
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 8, 2024
2 parents 7c989b4 + 3f9db77 commit 6021982
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)|||
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
@@ -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<Char, List<IntPair>>
init {
val lines = input.trimEnd().lines()
height = lines.size
width = lines.maxOfOrNull { it.length } ?: 0
antennae = buildMap<Char, MutableList<IntPair>> {
for ((y, line) in lines.withIndex()) {
for ((x, char) in line.withIndex()) {
if (char != '.') getOrPut(char) { mutableListOf() }.add(y to x)
}
}
}
}

private fun MutableCollection<IntPair>.addIfInRange(y: Int, x: Int): Boolean =
if (y in 0..<height && x in 0..<width) {
add(y to x)
true
} else false

private fun solve(allMultiples: Boolean): Int = buildSet {
for ((char, points) in antennae) {
for (p0 in points) {
for (p1 in points) {
if (p0 == p1) continue
val dy = p1.first - p0.first
val dx = p1.second - p0.second
if (!allMultiples) {
addIfInRange(p1.first + dy, p1.second + dx)
} else {
var i = 0
while (addIfInRange(p1.first + i * dy, p1.second + i * dx)) i++
}
}
}
}
}.size

fun part1() = solve(false)

fun part2() = solve(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ val days: List<Day> = 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 6021982

Please sign in to comment.