generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2015D09.kt
50 lines (41 loc) · 1.68 KB
/
Y2015D09.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 aockt.y2015
import aockt.util.generatePermutations
import io.github.jadarma.aockt.core.Solution
object Y2015D09 : Solution {
private val inputRegex = Regex("""^(\w+) to (\w+) = (\d+)$""")
/** Parses a single line of input and returns a triple containing two locations and the distance between them. */
private fun parseInput(input: String): Triple<String, String, Int> {
val (from, to, distance) = inputRegex.matchEntire(input)!!.destructured
return Triple(from, to, distance.toInt())
}
/**
* Given a list of distances between all pairs of locations on the map, returns all possible paths that visit all
* of them and their total distance.
*/
private fun bruteForceRoutes(locationData: List<Triple<String, String, Int>>): Sequence<Pair<List<String>, Int>> {
val locations = mutableSetOf<String>()
val distances = mutableMapOf<Pair<String, String>, Int>()
locationData.forEach { (from, to, distance) ->
locations.add(from)
locations.add(to)
distances[from to to] = distance
distances[to to from] = distance
}
return locations
.toList()
.generatePermutations()
.map { route -> route to route.windowed(2).sumOf { distances.getValue(it[0] to it[1]) } }
}
override fun partOne(input: String) =
input
.lines()
.map(this::parseInput)
.let(this::bruteForceRoutes)
.minOf { it.second }
override fun partTwo(input: String) =
input
.lines()
.map(this::parseInput)
.let(this::bruteForceRoutes)
.maxOf { it.second }
}