Created
December 19, 2021 13:23
-
-
Save ende76/18c36aac9da3d3cf68ad53956b746fb8 to your computer and use it in GitHub Desktop.
Kotlin solution for https://adventofcode.com/2021/day/18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.* | |
private fun readChar(): Char = readString()[0] | |
private fun readChars(): CharArray = readString().toCharArray() | |
private fun readInt(): Int = readLine()!!.toInt() | |
private fun readInts(): List<Int> = readLine()!!.trim().split(" ").map(String::toInt) | |
private fun readLong(): Long = readLine()!!.toLong() | |
private fun readLongs(): List<Long> = readLine()!!.trim().split(" ").map(String::toLong) | |
private fun readString(): String = readLine()!!.trim() | |
private fun readStrings(): List<String> = readLine()!!.trim().split(" ") | |
fun explode(tokens: MutableList<String>, i: Int) { | |
// println("explode ${tokens[i]+tokens[i+1]+tokens[i+2]+tokens[i+3]+tokens[i+4]}") | |
val explodeLeft = tokens[i + 1].toInt() | |
val explodeRight = tokens[i + 3].toInt() | |
tokens.removeAt(i) // [ | |
tokens.removeAt(i) // num | |
tokens.removeAt(i) // , | |
tokens.removeAt(i) // num | |
tokens.removeAt(i) // ] | |
tokens.add(i, "0") | |
for (j in i-1 downTo 0) { | |
val num = tokens[j].toIntOrNull() ?: continue | |
tokens[j] = (num + explodeLeft).toString() | |
break | |
} | |
for (j in i+1 until tokens.size) { | |
val num = tokens[j].toIntOrNull() ?: continue | |
tokens[j] = (num + explodeRight).toString() | |
break | |
} | |
} | |
fun split(tokens: MutableList<String>, i: Int) { | |
val num = tokens[i].toInt() | |
// println("split $num") | |
tokens.removeAt(i) | |
tokens.add(i, "]") | |
tokens.add(i, ((num + 1) / 2).toString()) | |
tokens.add(i, ",") | |
tokens.add(i, (num / 2).toString()) | |
tokens.add(i, "[") | |
} | |
fun reduce(tokens: MutableList<String>) : Boolean { | |
var nestedLevel = 0 | |
for ((i, t) in tokens.withIndex()) { | |
when (t) { | |
"[" -> { | |
nestedLevel += 1 | |
if (nestedLevel >= 5) { | |
explode(tokens, i) | |
// println(tokens.joinToString("")) | |
return true | |
} | |
} | |
"]" -> nestedLevel -= 1 | |
else -> continue | |
} | |
} | |
for ((i, t) in tokens.withIndex()) { | |
when (t) { | |
"[" -> continue | |
"]" -> continue | |
"," -> continue | |
else -> { | |
val num = t.toInt() | |
if (num >= 10) { | |
split(tokens, i) | |
// println(tokens.joinToString("")) | |
return true | |
} | |
} | |
} | |
} | |
return false | |
} | |
fun add(numbers: List<String>) : String { | |
var sum = numbers[0] | |
var tokens = mutableListOf<String>() | |
for (i in 1 until numbers.size) { | |
// println(" $sum\n+ ${numbers[i]}") | |
sum = "[$sum,${numbers[i]}]" | |
val tokenizer = StringTokenizer(sum, "[],", true) | |
tokens = tokenizer.toList().toMutableList() as MutableList<String> | |
while (reduce(tokens)) { | |
// println(tokens.joinToString("")) | |
} | |
sum = tokens.joinToString("") | |
// println("= $sum\n") | |
} | |
return sum | |
} | |
fun magnitude(tokens: List<String>, i: Int = 0) : Pair<Long, Int> = | |
if (tokens[i] == "[") { | |
val (leftMagnitude, j) = magnitude(tokens, i + 1) | |
val (rightMagnitude, k) = magnitude(tokens, j + 1) | |
Pair(3 * leftMagnitude + 2 * rightMagnitude, k + 1) | |
} else Pair(tokens[i].toLong(), i + 1) | |
fun main() { | |
var nextLine = readString() | |
val numbers = mutableListOf<String>() | |
nextLine@ while (nextLine.isNotEmpty()) { | |
numbers.add(nextLine) | |
nextLine = readString() | |
} | |
var max = 0L | |
for (i in numbers.indices) { | |
for (j in numbers.indices) { | |
if (i == j) continue | |
val sum = add(listOf(numbers[i], numbers[j])) | |
val tokenizer = StringTokenizer(sum, "[],", true) | |
val tokens = tokenizer.toList() as List<String> | |
max = max.coerceAtLeast(magnitude(tokens).first) | |
} | |
} | |
println(max) | |
} | |
/* | |
[1,1] | |
[2,2] | |
[3,3] | |
[4,4] | |
[[[[1,1],[2,2]],[3,3]],[4,4]] | |
[1,1] | |
[2,2] | |
[3,3] | |
[4,4] | |
[5,5] | |
[[[[3,0],[5,3]],[4,4]],[5,5]] | |
[1,1] | |
[2,2] | |
[3,3] | |
[4,4] | |
[5,5] | |
[6,6] | |
[[[[5,0],[7,4]],[5,5]],[6,6]] | |
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]] | |
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]] | |
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]] | |
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]] | |
[7,[5,[[3,8],[1,4]]]] | |
[[2,[2,2]],[8,[8,1]]] | |
[2,9] | |
[1,[[[9,3],9],[[9,0],[0,7]]]] | |
[[[5,[7,4]],7],1] | |
[[[[4,2],2],6],[8,7]] | |
[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] | |
[[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]] | |
[[[[4,3],4],4],[7,[[8,4],9]]] | |
[1,1] | |
after explode: [[[[0,7],4],[7,[[8,4],9]]],[1,1]] | |
after explode: [[[[0,7],4],[15,[0,13]]],[1,1]] | |
after split: [[[[0,7],4],[[7,8],[0,13]]],[1,1]] | |
after split: [[[[0,7],4],[[7,8],[0,[6,7]]]],[1,1]] | |
after explode: [[[[0,7],4],[[7,8],[6,0]]],[8,1]] | |
Magnitude examples: | |
[[1,2],[[3,4],5]] | |
[[[[0,7],4],[[7,8],[6,0]]],[8,1]] | |
[[[[1,1],[2,2]],[3,3]],[4,4]] | |
[[[[3,0],[5,3]],[4,4]],[5,5]] | |
[[[[5,0],[7,4]],[5,5]],[6,6]] | |
[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] | |
143 | |
1384 | |
445 | |
791 | |
1137 | |
3488 | |
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]] | |
[[[5,[2,8]],4],[5,[[9,9],0]]] | |
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]] | |
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]] | |
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]] | |
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]] | |
[[[[5,4],[7,7]],8],[[8,3],8]] | |
[[9,3],[[9,9],[6,[4,9]]]] | |
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] | |
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment