-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.kt
146 lines (121 loc) · 3.36 KB
/
Main.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
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
142
143
144
145
146
package y2022.day13
import InputLoader
import Year
import io.kotest.matchers.shouldBe
import kotlin.math.min
private sealed interface Elem : Comparable<Elem> {
@JvmInline
value class EList(val value: List<Elem>) : Elem {
override fun compareTo(other: Elem): Int {
return when (other) {
is EInt -> compareTo(EList(other))
is EList -> {
for (i in 0 until min(value.size, other.value.size)) {
value[i].compareTo(other.value[i]).let {
if (it != 0) return it
}
}
value.size.compareTo(other.value.size)
}
}
}
constructor(value: Elem) : this(listOf(value))
}
@JvmInline
value class EInt(val value: Int) : Elem {
override fun compareTo(other: Elem): Int =
when (other) {
is EInt -> value.compareTo(other.value)
is EList -> EList(this).compareTo(other)
}
}
}
private fun ListIterator<Char>.parseDigit(): Elem.EInt =
buildString {
while (true) {
when (val char = next()) {
in '0'..'9' -> append(char)
else -> break
}
}
}.let {
previous()
return Elem.EInt(it.toInt())
}
private fun ListIterator<Char>.parseList(): Elem.EList {
val items = mutableListOf<Elem>()
next() shouldBe '['
while (true) {
when (val char = next()) {
'[' -> {
previous()
items.add(parseList())
}
']' -> return Elem.EList(items)
',' -> {}
in '0'..'9' -> {
previous()
items.add(parseDigit())
}
else -> error("Unexpected character $char")
}
}
}
private fun String.parseInputPairs() =
lines()
.chunked(3)
.map { chunk ->
chunk[0].toList().listIterator().parseList() to
chunk[1].toList().listIterator().parseList()
}
private fun String.part01(): Int =
parseInputPairs()
.mapIndexed { index, (left, right) ->
if (left.compareTo(right) != 1) index + 1
else 0
}.sum()
private fun String.parseInputLines() =
lineSequence()
.filter { it.isNotBlank() }
.map { line ->
line.toList().listIterator().parseList()
}
private fun String.part02(): Int =
parseInputLines().let { input ->
val marks = listOf(
Elem.EList(Elem.EList(Elem.EInt(2))),
Elem.EList(Elem.EList(Elem.EInt(6))),
)
(input + marks).sorted().toList().let { sorted ->
marks.map { mark ->
sorted.binarySearch(mark) + 1
}.let { (f, s) -> f * s }
}
}
fun main() {
testInput.part01() shouldBe PART_01_RES
testInput.part02() shouldBe PART_02_RES
val input = InputLoader.loadInput(Year.Y2022, "day13")
println(input.part01())
println(input.part02())
}
private val testInput = """
[1,1,3,1,1]
[1,1,5,1,1]
[[1],[2,3,4]]
[[1],4]
[9]
[[8,7,6]]
[[4,4],4,4]
[[4,4],4,4,4]
[7,7,7,7]
[7,7,7]
[]
[3]
[[[]]]
[[]]
[1,[2,[3,[4,[5,6,7]]]],8,9]
[1,[2,[3,[4,[5,6,0]]]],8,9]
""".trimIndent()
private const val PART_01_RES = 13
private const val PART_02_RES = 140