-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay13.kt
More file actions
64 lines (53 loc) 路 2.4 KB
/
Copy pathDay13.kt
File metadata and controls
64 lines (53 loc) 路 2.4 KB
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
private sealed class ListOrInt : Comparable<ListOrInt> {
override fun compareTo(other: ListOrInt): Int = when (this) {
is WrappedInt -> when (other) {
is WrappedInt -> value compareTo other.value
is WrappedList -> WrappedList(this) compareTo other
}
is WrappedList -> when (other) {
is WrappedInt -> -(other compareTo this)
is WrappedList -> list.asSequence()
.zip(other.list.asSequence())
.map { (a, b) -> a compareTo b }
.find { it != 0 }
?: (list.size compareTo other.list.size)
}
}
companion object {
fun fromString(string: String) = parseFromString(string).first
private fun parseFromString(string: String): Pair<ListOrInt, String> {
return if (string[0].isDigit()) {
val numberString = string.takeWhile { it.isDigit() }
WrappedInt(numberString.toInt()) to string.substring(numberString.length)
} else {
var currentString = string.substring(1)
val list = buildList {
while (currentString.isNotEmpty() && currentString[0] != ']') {
val (element, newString) = parseFromString(currentString)
add(element)
currentString = newString.trimStart(',')
}
}
WrappedList(list) to currentString.drop(1)
}
}
}
}
private data class WrappedList(val list: List<ListOrInt>) : ListOrInt() {
constructor(vararg listOrInt: ListOrInt) : this(listOrInt.toList())
}
private data class WrappedInt(val value: Int) : ListOrInt()
private fun part1(pairs: List<Pair<ListOrInt, ListOrInt>>) = pairs.asSequence()
.withIndex()
.filter { (_, i) -> i.first compareTo i.second <= 0 }
.sumOf { it.index + 1 }
private fun part2(pairs: List<Pair<ListOrInt, ListOrInt>>): Int {
val additional = listOf(WrappedList(WrappedList(WrappedInt(2))), WrappedList(WrappedList(WrappedInt(6))))
val sorted = pairs.asSequence().flatMap { it.toList() }.let { it + additional }.sorted().toList()
return additional.asSequence().map { sorted.indexOf(it) + 1 }.reduce(Int::times)
}
fun main() {
val pairs = mapBlocks { it.asSequence().map(ListOrInt::fromString).toList().toPair() }
println(part1(pairs))
println(part2(pairs))
}