-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path09.kt
24 lines (23 loc) · 1.03 KB
/
09.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
operator fun Pair<Int, Int>.plus(o: Pair<Int, Int>) = Pair(this.first + o.first, this.second + o.second)
fun main() {
val input = generateSequence(::readlnOrNull).toList().map { it.split(" ") }
val visited2: MutableSet<Pair<Int, Int>> = mutableSetOf()
val visited10: MutableSet<Pair<Int, Int>> = mutableSetOf()
val lookup = mapOf("D" to Pair(0, 1), "U" to Pair(0, -1), "L" to Pair(-1, 0), "R" to Pair(1, 0))
val rope = MutableList(10) { Pair(0, 0) }
for ((dir, steps) in input) {
for (j in 1..steps.toInt()) {
rope[0] = rope[0] + lookup[dir]!!
for (i in 0..rope.size-2) {
val dx = rope[i].first - rope[i+1].first
val dy = rope[i].second - rope[i+1].second
if (Math.abs(dx) >= 2 || Math.abs(dy) >= 2)
rope[i+1] = rope[i+1] + Pair(dx.coerceIn(-1..1), dy.coerceIn(-1..1))
}
visited2.add(rope[1])
visited10.add(rope.last())
}
}
println(visited2.size)
println(visited10.size)
}