-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.kt
45 lines (39 loc) · 1.29 KB
/
Day5.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import kotlin.math.abs
object Day5 : AdventDay() {
override fun solve() {
val lines = reads<String>()?.map { it.toLine() } ?: return
Diagram().apply {
lines.filter { it.isVertical || it.isHorizontal }.forEach { markLine(it) }
marked.count { it.value > 1 }.printIt()
}
Diagram().apply {
lines.forEach { markLine(it) }
marked.count { it.value > 1 }.printIt()
}
}
}
private data class P(val x: Int, val y: Int)
private data class L(val from: P, val to: P) {
val isHorizontal = from.x == to.x
val isVertical = from.y == to.y
val isDiagonal = abs(from.y - to.y) == abs(from.x - to.x)
}
private fun String.toLine() = split(" -> ").map { p ->
p.split(",").let { (x, y) -> P(x.toInt(), y.toInt()) }
}.let { (f, t) -> L(f, t) }
private class Diagram {
private val _m = DefaultMap<P, Int>(0)
val marked: Map<P, Int> = _m
fun markLine(line: L) = with(line) {
when {
isVertical -> (from.x directedTo to.x).map { P(it, to.y) }
.forEach { _m[it] = _m[it] + 1 }
isHorizontal -> (from.y directedTo to.y).map { P(to.x, it) }
.forEach { _m[it] = _m[it] + 1 }
isDiagonal -> (from.x directedTo to.x).zip(from.y directedTo to.y)
.map { (x, y) -> P(x, y) }
.forEach { _m[it] = _m[it] + 1 }
else -> Unit
}
}
}