advent-of-code/2018/07/main.kt

29 lines
1.0 KiB
Kotlin
Raw Normal View History

2018-12-07 09:19:20 +01:00
import java.io.File
import java.io.InputStream
fun main(args: Array<String>) {
val inputStream = File("input").inputStream()
val inputLines = inputStream.bufferedReader().use { it.readText() }.split("\n")
var map: HashMap<Char, HashSet<Char>> = HashMap()
for (line in inputLines) {
val step = line[36]
val dep = line[5]
// Make sure values that are only referenced as dependencies
// (i. e. the first step in our chain) are also added to the set.
map.getOrPut(dep, { HashSet<Char>() })
val currentDeps = map.getOrPut(step, { HashSet<Char>() })
currentDeps.add(dep)
}
2018-12-07 11:08:01 +01:00
var output = mutableListOf<Char>()
2018-12-07 09:19:20 +01:00
while (map.size > 0) {
2018-12-07 11:08:01 +01:00
// The keyset in Kotlin is already sorted, so we can just take the first value
val next = map.entries.map{ e -> Pair(e.key, e.value.size) }.filter{ e -> e.second == 0 }.map{ e -> e.first }[0]
map.remove(next)
map.values.map{ v -> v.remove(next) }
output.add(next)
2018-12-07 09:19:20 +01:00
}
2018-12-07 11:08:01 +01:00
println(output.joinToString(""))
2018-12-07 09:19:20 +01:00
}