-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc-13.kts
98 lines (86 loc) · 2.68 KB
/
aoc-13.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
// https://adventofcode.com/2022/day/13
import java.io.File
fun input() = File("input/aoc-13.txt").readLines()
// Part 1.
data class Node(val value: Int?, val children: List<Node>?): Comparable<Node> {
override fun toString(): String = when {
value != null -> value.toString()
else -> "[${children!!.joinToString(",")}]"
}
fun length(): Int = toString().length
override fun compareTo(other: Node) = when (compare(this, other)) {
null -> 0
true -> -1
else -> 1
}
}
fun parse(s: String, n: Int, i: Int): Node? {
when {
i >= n || s[i] == ',' || s[i] == ']' -> {
return null
}
s[i] >= '0' && s[i] <= '9' -> {
for (j in (i+1)..n) {
if (s[j] < '0' || s[j] > '9') return Node(s.substring(i, j).toInt(), null)
}
}
s[i] == '[' -> {
var j = i+1
val children = mutableListOf<Node>()
while (true) {
val c = parse(s, n, j)
if (c == null) {
break
} else {
children.add(c)
j += c.length() + 1
}
}
return Node(null, children)
}
else -> error("oops")
}
error("oops")
}
fun compare(a: Node, b: Node): Boolean? {
when {
a.value != null && b.value != null -> {
return if (a.value == b.value) null else a.value < b.value
}
a.value == null && b.value != null -> {
return compare(a, Node(null, listOf(b)))
}
a.value != null && b.value == null -> {
return compare(Node(null, listOf(a)), b)
}
a.value == null && b.value == null -> {
val an = a.children!!.size
val bn = b.children!!.size
val n = if (an < bn) an else bn
for (i in 0 until n) {
compare(a.children[i], b.children[i])?.let { return it }
}
return if (an == bn) null else an < bn
}
else -> error("oops")
}
}
fun p1(lines: List<String>): Int {
var z = 0
for (i in 0..lines.size step 3) {
val a = parse(lines[i], lines[i].length, 0)!!
val b = parse(lines[i+1], lines[i+1].length, 0)!!
if (compare(a, b) == true) z += 1 + (i / 3)
}
return z
}
// Part 2.
fun p2(lines: List<String>): Int {
val packets = lines.filter { it != "" } + "[[2]]" + "[[6]]"
val res = packets.map { parse(it, it.length, 0)!! }.sorted().map { it.toString() }
return (res.indexOf("[[2]]") + 1) * (res.indexOf("[[6]]") + 1)
}
with(input()) {
println(p1(this)) // 5682
println(p2(this)) // 20304
}