-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.kt
52 lines (41 loc) · 1.51 KB
/
Day12.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
import kotlin.math.abs
fun main() {
val directions = mapLines { it[0] to it.drop(1).toInt() }
var first = 0
var angle = 0
for ((direction, distance) in directions) {
when {
direction in "NW" || direction == 'F' && angle in 180..270 -> first += distance
direction in "SE" || direction == 'F' && angle in 0..90 -> first -= distance
direction == 'L' -> angle = (angle - distance + 360) % 360
direction == 'R' -> angle = (angle + distance) % 360
}
}
first = abs(first)
var shipX = 0
var shipY = 0
var waypointX = -10
var waypointY = 1
for ((command, distance) in directions) {
when {
command == 'N' -> waypointY += distance
command == 'S' -> waypointY -= distance
command == 'E' -> waypointX -= distance
command == 'W' -> waypointX += distance
(command to distance).let { it == 'L' to 90 || it == 'R' to 270 } ->
waypointX = waypointY.also { waypointY = -waypointX }
(command to distance).let { it == 'L' to 270 || it == 'R' to 90 } ->
waypointX = -waypointY.also { waypointY = waypointX }
command == 'F' -> {
shipX += distance * waypointX
shipY += distance * waypointY
}
else -> {
waypointX *= -1
waypointY *= -1
}
}
}
val second = abs(shipX) + abs(shipY)
println("$first $second")
}