-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day09.kt
81 lines (63 loc) · 2.44 KB
/
Day09.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package aoc.years.year2022
import aoc.Day
import kotlin.math.abs
@Year2022
class Day09 : Day() {
override fun solvePart1(input: List<String>): Any {
val bridge = Bridge()
input
.map { it.split(" ") }
.forEach { (direction, steps) -> bridge.moveHead(direction, steps.toInt()) }
return bridge.ropeEndPositions.size
}
override fun solvePart2(input: List<String>): Any {
val bridge = Bridge()
repeat(8) { bridge.rope.add(Pair(0, 0)) }
input
.map { it.split(" ") }
.forEach { (direction, steps) -> bridge.moveHead(direction, steps.toInt()) }
return bridge.ropeEndPositions.size
}
}
private class Bridge {
var rope = mutableListOf(Pair(0, 0), Pair(0, 0))
var ropeEndPositions = mutableSetOf(rope.last())
fun moveHead(direction: String, steps: Int) {
repeat(steps) {
moveHead(direction)
(1 until rope.size)
.forEach { rope[it] = moveTail(rope[it], rope[it - 1]) }
ropeEndPositions.add(rope.last())
}
}
private fun moveHead(direction: String) {
rope[0] = when (direction) {
"R" -> Pair(rope[0].first, rope[0].second + 1)
"D" -> Pair(rope[0].first - 1, rope[0].second)
"L" -> Pair(rope[0].first, rope[0].second - 1)
"U" -> Pair(rope[0].first + 1, rope[0].second)
else -> throw IllegalArgumentException("Unknown direction $direction")
}
}
private fun moveTail(tail: Pair<Int, Int>, head: Pair<Int, Int>): Pair<Int, Int> {
if (tailNotTouchingHead(tail, head)) {
return if (tail.first == head.first) {
// Same row
Pair(tail.first, tail.second + if (tail.second < head.second) 1 else -1)
} else if (tail.second == head.second) {
// Same column
Pair(tail.first + if (tail.first < head.first) 1 else -1, tail.second)
} else {
// Move diagonally
Pair(
tail.first + if (tail.first < head.first) 1 else -1,
tail.second + if (tail.second < head.second) 1 else -1
)
}
}
return tail
}
private fun tailNotTouchingHead(tail: Pair<Int, Int>, head: Pair<Int, Int>): Boolean {
return abs(tail.first.minus(head.first)) > 1 || abs(tail.second.minus(head.second)) > 1
}
}