-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.kts
141 lines (116 loc) · 3.99 KB
/
07.kts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// null checks is nice, if I were a Java developer I'd
// hop onto this immediately. Java has a heavy
// interfaces system though
import java.io.File
import java.util.LinkedList
// to run:
// kotlinc -script 7.kts
class Solver() {
val inverseTree = HashMap<String, MutableSet<String>>()
val tree = HashMap<String, MutableMap<String, Int>>()
val selfAndDescendantCounts = HashMap<String, Int>()
val lineRegex = Regex("""(.+) bags contain (.+).""")
val childRegex = Regex("""(\d+) (.+) bags?""")
fun solve() {
parseInput("7.input")
printTree()
//printInverseTree()
printDescendantCount("shiny gold")
//printParents("shiny gold")
}
private fun printDescendantCount(type: String) {
val count = selfAndDescendantCount(type) - 1
println("$type descendantCount: $count")
}
private fun selfAndDescendantCount(type: String): Int {
val cachedCount = selfAndDescendantCounts[type]
if (cachedCount !== null) { return cachedCount }
var count = 1
val children = tree[type]
if (children != null) {
for ((childType, childCount) in children) {
count += (childCount * selfAndDescendantCount(childType))
}
}
selfAndDescendantCounts[type] = count
return count
}
private fun printParents(child: String) {
val allParents = HashSet<String>()
val queue = LinkedList<String>()
queue.addLast(child)
while (queue.size > 0) {
val current = queue.removeFirst()
val currentParents = inverseTree[current]
if (currentParents != null) {
for (currentParent in currentParents) {
if (!allParents.contains(currentParent)) {
allParents.add(currentParent)
queue.addLast(currentParent)
}
}
}
}
println("$child allParents:")
for (parent in allParents) {
println(parent)
}
println(allParents.size)
}
private fun printTree() {
println("tree:")
for ((parent, children) in tree) {
println("'$parent'")
for ((childType, childValue) in children) {
println("'$childType' $childValue")
}
println()
}
}
private fun printInverseTree() {
println("inverseTree:")
for ((child, parents) in inverseTree) {
println("'$child'")
for (parent in parents) {
println("'$parent'")
}
println()
}
}
private fun parseInput(path: String) {
File(path).forEachLine {
val matchResult = lineRegex.find(it)
val groups = matchResult!!.groups
val parent = groups.get(1)!!.value
val childStrings = groups.get(2)!!.value.split(", ")
parseLine(parent, childStrings)
}
}
private fun parseLine(parent: String, childStrings: Collection<String>) {
//println(parent)
var children = tree[parent]
if (children == null) {
children = HashMap<String, Int>()
tree[parent] = children
}
for(childString in childStrings) {
//println(childString)
val matchResult = childRegex.find(childString)
if (matchResult == null) { continue } // contain no other bags case
val childCount = matchResult.groups.get(1)!!.value.toInt()
val childType = matchResult.groups.get(2)!!.value
//println(childType)
children[childType] = childCount
var parents = inverseTree[childType]
if (parents == null) {
parents = HashSet<String>()
parents.add(parent)
inverseTree[childType] = parents
} else {
parents.add(parent)
}
}
//println()
}
}
Solver().solve()