-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.kt
50 lines (43 loc) · 1.53 KB
/
Day2.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
package aoc.puzzles
/**
* @author Kris | 05/12/2021
*/
object Day2 : Puzzle<List<String>, Long>(2) {
private val UP = Instruction("up")
private val DOWN = Instruction("down")
private val FORWARD = Instruction("forward")
override fun List<String>.parse() = this
override fun List<String>.solvePartOne(): Long {
val forwardSum = sumOf(Regex("""$FORWARD (\d+)"""))
val upSum = sumOf(Regex("""$UP (\d+)"""))
val downSum = sumOf(Regex("""$DOWN (\d+)"""))
return forwardSum.toLong() * (downSum - upSum)
}
override fun List<String>.solvePartTwo(): Long = with(Submarine()) {
toInstructions().forEach { (instruction, count) ->
when (instruction) {
UP -> aim -= count
DOWN -> aim += count
FORWARD -> {
horizontalPosition += count
depth += aim * count
}
}
}
return horizontalPosition * depth
}
private fun List<String>.toInstructions(): List<Pair<Instruction, Int>> = map {
val (instruction, count) = it.split(' ')
Instruction(instruction) to count.toInt()
}
private fun List<String>.sumOf(regex: Regex): Int = this@sumOf.sumOf { regex.find(it)?.groupValues?.get(1)?.toInt() ?: 0 }
}
private data class Submarine(
var aim: Long = 0,
var horizontalPosition: Long = 0,
var depth: Long = 0
)
@JvmInline
private value class Instruction(private val instruction: String) {
override fun toString() = instruction
}