-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.kt
65 lines (53 loc) · 2.16 KB
/
Day13.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
package aoc.years.year2022
import aoc.Day
import com.beust.klaxon.JsonArray
import com.beust.klaxon.Parser.Companion.default
private val JSON_PARSER = default()
@Year2022
class Day13 : Day() {
override fun solvePart1(input: List<String>): Any {
return input
.chunked(3)
.mapIndexed { nr, (p1, p2, _) ->
val packet1 = JSON_PARSER.parse(StringBuilder(p1)) as JsonArray<*>
val packet2 = JSON_PARSER.parse(StringBuilder(p2)) as JsonArray<*>
if (packet1 compareTo packet2 < 0) nr + 1 else 0
}
.sum()
}
override fun solvePart2(input: List<String>): Any {
val sortedPackets = input
.asSequence()
.chunked(3)
.map { (p1, p2, _) -> listOf(p1, p2) }
.flatten()
.plus(listOf("[[2]]", "[[6]]"))
.map { JSON_PARSER.parse(StringBuilder(it)) as JsonArray<*> }
.sortedWith { p1, p2 -> p1 compareTo p2 }
return sortedPackets.indexOfFirst { it.toString() == "JsonArray(value=[JsonArray(value=[2])])" }.plus(1)
.times(sortedPackets.indexOfFirst { it.toString() == "JsonArray(value=[JsonArray(value=[6])])" }.plus(1))
}
}
private infix fun Any.compareTo(other: Any): Int {
if (this.toString().toIntOrNull() != null && other.toString().toIntOrNull() != null) {
return this.toString().toInt().compareTo(other.toString().toInt())
}
if (this is JsonArray<*> || other is JsonArray<*>) {
// Wrap in list if single element
val newThis = if (this is JsonArray<*>) this else JsonArray(this)
val newOther = if (other is JsonArray<*>) other else JsonArray(other)
for (i in newThis.indices) {
if (i in newOther.indices) {
val comparison = newThis[i]!! compareTo newOther[i]!!
if (comparison == 0) {
continue
}
return comparison
} else {
return 1 // Left side is longer
}
}
return if (newThis.size == newOther.size) 0 else -1 // Equal size or right side is longer
}
return 0
}