-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay09AllInASingleNight.kt
38 lines (30 loc) · 1.09 KB
/
Day09AllInASingleNight.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
package adventofcode.year2015
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.Permutations.permutations
class Day09AllInASingleNight(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override val name = "All in a Single Night"
private val distances by lazy {
input
.lines()
.associate {
val (cityA, cityB, distance) = INPUT_REGEX.find(it)!!.destructured
setOf(cityA, cityB) to distance.toInt()
}
}
private val cities by lazy { distances.keys.flatten().toSet() }
private val routes by lazy {
cities
.permutations()
.map { route ->
route.dropLast(1).foldIndexed(0) { index, distance, _ ->
distance + distances[setOf(route[index], route[index + 1])]!!
}
}
}
override fun partOne() = routes.minOrNull() ?: 0
override fun partTwo() = routes.maxOrNull() ?: 0
companion object {
private val INPUT_REGEX = """(\w+) to (\w+) = (\d+)""".toRegex()
}
}