diff options
author | tchojnacki <tomaszchojnacki2001@gmail.com> | 2022-08-11 19:24:23 +0200 |
---|---|---|
committer | tchojnacki <tomaszchojnacki2001@gmail.com> | 2022-08-11 19:24:23 +0200 |
commit | 0f1e145b80813ae2331b7dac5ace0c589654ad2a (patch) | |
tree | 25483b8239436dd5aed2fee8811caf0ba893c0bb /aoc-2021-kotlin/src/Day05.kt | |
parent | 85fb0396bed6a2129b12392941103924b1ab55be (diff) | |
download | gleam_aoc2020-0f1e145b80813ae2331b7dac5ace0c589654ad2a.tar.gz gleam_aoc2020-0f1e145b80813ae2331b7dac5ace0c589654ad2a.zip |
Move subproject to avoid IntelliJ module name issues
Diffstat (limited to 'aoc-2021-kotlin/src/Day05.kt')
-rw-r--r-- | aoc-2021-kotlin/src/Day05.kt | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/aoc-2021-kotlin/src/Day05.kt b/aoc-2021-kotlin/src/Day05.kt new file mode 100644 index 0000000..2c5e219 --- /dev/null +++ b/aoc-2021-kotlin/src/Day05.kt @@ -0,0 +1,62 @@ +import kotlin.math.absoluteValue +import kotlin.math.max +import kotlin.math.sign + +object Day05 { + private data class Line(val start: Pos2D, val end: Pos2D) { + companion object { + fun fromString(input: String): Line { + val (start, end) = input.split(" -> ").map { coordinateString -> + val (x, y) = coordinateString.split(",").map(String::toInt) + Pos2D(x, y) + } + + return Line(start, end) + } + } + + val isHorizontalOrVertical: Boolean + get() = start.x == end.x || start.y == end.y + + val isDiagonal: Boolean + get() = (end.x - start.x).absoluteValue == (end.y - start.y).absoluteValue + + val pointSequence: Sequence<Pos2D> + get() = sequence { + val xOffset = end.x - start.x + val yOffset = end.y - start.y + + for (s in 0..max(xOffset.absoluteValue, yOffset.absoluteValue)) { + val x = start.x + s * xOffset.sign + val y = start.y + s * yOffset.sign + + yield(Pos2D(x, y)) + } + } + } + + private fun helper(input: List<String>, linePredicate: (line: Line) -> Boolean) = input + .asSequence() + .map(Line::fromString) + .filter(linePredicate) + .flatMap(Line::pointSequence) + .groupingBy { it } + .eachCount() + .values + .count { it >= 2 } + + fun part1(input: List<String>): Int = helper(input, Line::isHorizontalOrVertical) + + fun part2(input: List<String>): Int = helper(input) { it.isHorizontalOrVertical || it.isDiagonal } + +} + +fun main() { + val testInput = readInputAsLines("Day05_test") + check(Day05.part1(testInput) == 5) + check(Day05.part2(testInput) == 12) + + val input = readInputAsLines("Day05") + println(Day05.part1(input)) + println(Day05.part2(input)) +} |