-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from ephemient/kt/day8
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day8Bench.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day8Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |