-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay7.kt
58 lines (47 loc) · 1.95 KB
/
Day7.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
package aoc2018.day07
import util.readInputLineByLine
fun readInputNumbers(path: String): List<Step> {
val stepList = mutableListOf<Step>()
val regex = Regex("Step ([A-Z]) must be finished before step ([A-Z]) can begin.")
for (line in readInputLineByLine(path)) {
val matchResult = regex.matchEntire(line) ?: throw RuntimeException()
stepList.add(Step(matchResult.groupValues[1], matchResult.groupValues[2]))
}
return stepList
}
fun completeAllSteps(list: List<Step>, workers: Int, offset: Int): Int {
val allSteps = list.flatMap { listOf(it.first, it.second) }.distinct()
val completed = sortSteps(allSteps, list).toMutableList()
val running = mutableListOf<Pair<String, Int>>()
completed.clear()
var seconds = 0;
while (completed.size < allSteps.size) {
val done = running.filter { it.second == seconds }
completed.addAll(done.map { it.first })
running.removeAll(done)
val canStart = (allSteps - completed - running.map { it.first })
.filterNot { sec -> list.any { it.second == sec && !completed.contains(it.first) } }
.sorted()
running.addAll(canStart
.take(workers - running.size)
.map { Pair(it, seconds + offset + (it[0] - 64).toInt()) }
)
seconds++
}
return seconds - 1
}
fun determineStepOrder(list: List<Step>): String {
val allSteps = list.flatMap { listOf(it.first, it.second) }.distinct()
return sortSteps(allSteps, list).joinToString("")
}
private fun sortSteps(allSteps: List<String>, list: List<Step>): List<String> {
val completed = mutableListOf<String>()
while (completed.size < allSteps.size) {
val canStart = (allSteps - completed)
.filterNot { s -> list.any { it.second == s && !completed.contains(it.first) } }
.sorted()
completed.add(canStart.first())
}
return completed
}
data class Step(val first: String, val second: String)