-
Notifications
You must be signed in to change notification settings - Fork 0
/
19a.kt
57 lines (51 loc) · 1.47 KB
/
19a.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
import kotlin.io.path.Path
import kotlin.io.path.readText
import kotlin.math.*
fun main() {
val fileName = "19.in"
val blocks = Path(fileName).readText().split("\n\n")
val rules = blocks[0].split("\n")
val items = blocks[1].split("\n")
var result = 0
var R = mutableMapOf<String, List<String>>()
for (rule in rules) {
val parts = rule.split("{")
val name = parts[0]
val rest = parts[1].dropLast(1)
val commands = rest.split(",")
R[name] = commands
}
for (item in items) {
val values = item.substring(1, item.length - 1).split(",")
val (x, m, a, s) = values.map { it.split("=")[1].toInt() }
val valuesMap = mutableMapOf('x' to x, 'm' to m, 'a' to a, 's' to s)
var currentRule = "in"
while (currentRule !in listOf("A", "R")) {
for (command in R[currentRule]!!) {
if (":" in command) {
val (cond, nextRule) = command.split(":")
val variable = cond[0]
val limit = cond.slice(2..cond.length - 1).toInt()
if (cond.contains("<")) {
if (valuesMap[variable]!! < limit) {
currentRule = nextRule
break
}
}
if (cond.contains(">")) {
if (valuesMap[variable]!! > limit) {
currentRule = nextRule
break
}
}
} else {
currentRule = command
}
}
}
if (currentRule == "A") {
result += x + m + a + s
}
}
println(result)
}