-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_7_a.kt
44 lines (28 loc) · 1.12 KB
/
day_7_a.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
import java.io.File
import java.util.*
data class Node(val id: Char, val children: ArrayList<Node>, var discovered: Boolean = false, var finished: Boolean = false)
fun main(args: Array<String>) {
val regex = Regex("Step ([A-Z]) must be finished before step ([A-Z]) can begin\\.")
val nodes = HashMap<Char, Node>()
File(args[0]).forEachLine { line ->
val results = regex.find(line)!!.groupValues
val from = results[1][0]
val to = results[2][0]
nodes.computeIfAbsent(from, { t -> Node(t, ArrayList()) })
nodes.computeIfAbsent(to, { t -> Node(t, ArrayList()) })
nodes[to]?.let { nodes[from]!!.children.add(it) }
}
val list = LinkedList<Node>()
nodes.values.sortedBy { it.id }.reversed().forEach { dfs(it, list) }
list.forEach { print(it.id) }
}
fun dfs(node: Node, list: LinkedList<Node>) {
if(node.finished)
return
if(node.discovered)
throw Exception("Node $node already visited")
node.discovered = true
node.children.sortedBy { it.id }.reversed().forEach { dfs(it, list) }
node.finished = true
list.addFirst(node)
}