generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2016D01.kt
71 lines (60 loc) · 2.68 KB
/
Y2016D01.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
package aockt.y2016
import aockt.y2016.Y2016D01.Direction.*
import io.github.jadarma.aockt.core.Solution
import kotlin.math.absoluteValue
object Y2016D01 : Solution {
/** Parses the input and returns the sequence of turn and direction pairs. */
private fun parseInput(input: String): Sequence<Pair<Boolean, Int>> =
Regex("""(?:, )?([LR])(\d+)""")
.findAll(input)
.map {
val (turn, distance) = it.destructured
val clockwise = when (turn.first()) {
'R' -> true
'L' -> false
else -> throw IllegalArgumentException("Can only turn (L)eft or (R)ight.")
}
clockwise to distance.toInt()
}
/** The cardinal directions and their effect on grid movement. */
private enum class Direction(val horizontalMultiplier: Int, val verticalMultiplier: Int) {
North(0, 1), East(1, 0), South(0, -1), West(-1, 0)
}
/** Holds a position on the grid, and the direction you are facing. */
private data class Position(val x: Int, val y: Int, val direction: Direction)
/** Keep the current coordinates but change the direction by turning [clockwise] or not 90 degrees. */
private fun Position.turn(clockwise: Boolean) = copy(
direction = when (direction) {
North -> if (clockwise) West else East
East -> if (clockwise) North else South
South -> if (clockwise) East else West
West -> if (clockwise) South else North
}
)
/** Move [distance] units in the current [Position.direction]. */
private fun Position.move(distance: Int) = copy(
x = x + distance * direction.horizontalMultiplier,
y = y + distance * direction.verticalMultiplier,
)
/** Returns the distance between this position and the origin point (0,0). */
private val Position.distanceFromOrigin get() = x.absoluteValue + y.absoluteValue
override fun partOne(input: String) =
parseInput(input)
.fold(Position(0, 0, North)) { pos, instr -> pos.turn(instr.first).move(instr.second) }
.distanceFromOrigin
override fun partTwo(input: String): Int {
val visited = mutableSetOf(0 to 0)
parseInput(input)
.fold(Position(0, 0, North)) { pos, instr ->
(1..instr.second).fold(pos.turn(instr.first)) { it, _ ->
it.move(1).apply {
with(x to y) {
if (this in visited) return distanceFromOrigin
visited.add(this)
}
}
}
}
return -1
}
}