Advent of Code 2021 day 16
12/16/2021, 5:00 AMDavid Whittaker
12/16/2021, 6:20 AMMichael de Kaste
12/16/2021, 6:31 AMDavid Whittaker
12/16/2021, 6:34 AMStack
to keep track of to-be-calculated valuesDan Fingal-Surma
12/16/2021, 6:36 AMDan Fingal-Surma
12/16/2021, 6:41 AMelizarov
12/16/2021, 6:46 AMDan Fingal-Surma
12/16/2021, 7:09 AMerror: 'when' expression must be exhaustive, add necessary 'else' branch (problem_16.main.kts:59:40)
Guessing this is because I’m using kts?Dan Fingal-Surma
12/16/2021, 7:12 AMDan Fingal-Surma
12/16/2021, 7:25 AMIterator<Char>
nkiesel
12/16/2021, 8:37 AMData
class with offset:
class Data(private val data: String) {
var offset = 0
fun str(bits: Int) = data.substring(offset, offset + bits).also { offset += bits }
fun int(bits: Int) = str(bits).toInt(2)
}
That made the parser pretty simple (recursive descent).Dan Fingal-Surma
12/16/2021, 8:43 AM((425542 * (247 < 247)) + (121 + 21236) + (((11 + 12 + 11) > (7 + 10 + 7)) * 32566) + (((8 + 7 + 15) < (6 + 11 + 10)) * 4507180) + min(max(min(min(max(max(min(130))))))) + 139930778832 + ((52 > 667118) * 10) + 602147 + max(62199) + (14849899 * (11716 < 26963)) + (4083 * (135 > 135)) + (135 * 217 * 224) + 73 + ((13 + 4 + 9) * (12 + 15 + 7) * (13 + 10 + 9)) + min(194) + (182 * 197 * 136 * 2 * 242) + (226 * 142 * 34 * 124) + max(4025, 186042) + min(30059, 126119002) + min(9, 260, 162) + ((4 < 4) * 28699) + (1945 * (1714 == 1714)) + (7 * (1545 < 108)) + 12 + (200 * (31050 > 655605)) + 3154 + (3 * (64896 < 116)) + 3055 + 13 + min(48082, 226938, 1175, 68077774919) + (66 + 15 + 181 + 1380642642 + 11831587) + (241 * 59) + (150 * (2742 > 113)) + 37007908601 + max(52444, 11, 13008816, 2935) + 20723 + 8 + (5 * (6241732 > 759708)) + ((15 * 7 * 4) + (14 * 2 * 12) + (13 * 6 * 6)) + (2877 + 229333 + 655820 + 1020971) + (39581 + 2 + 14) + max(982557, 44, 31) + 68 + ((11530 == 3492) * 41177) + ((236 == 918711093) * 3937) + max(903466, 228, 6, 25989131, 4028) + 229 + min(299875, 10969849, 11481, 2281, 13) + (55300721 * (63 > 63)) + (244 * ((7 + 13 + 7) > (12 + 5 + 14))) + (4494263 * ((4 + 15 + 4) == (3 + 3 + 14))) + ((45 < 3307915) * 58514) + (3596530693 * ((3 + 12 + 4) < (9 + 11 + 2))))
nkiesel
12/16/2021, 9:01 AMelizarov
12/16/2021, 9:02 AMx.toString().padStart(n, '0')
nkiesel
12/16/2021, 9:02 AMelizarov
12/16/2021, 9:03 AMInt.toString()
had a few extra optional parameters to customize length and paddingelizarov
12/16/2021, 9:12 AMDan Fingal-Surma
12/16/2021, 9:15 AM(+ (* 425542 (< 247 247)) (+ 121 21236) (* (> (+ 11 12 11) (+ 7 10 7)) 32566) (* (< (+ 8 7 15) (+ 6 11 10)) 4507180) (min (* (* (+ (* (max (* (min (* (min (* (max (+ (+ (max (+ (+ (min (* (* 130)))))))))))))))))))) 139930778832 (* (> 52 667118) 10) 602147 (max 62199) (* 14849899 (< 11716 26963)) (* 4083 (> 135 135)) (* 135 217 224) 73 (* (+ 13 4 9) (+ 12 15 7) (+ 13 10 9)) (min 194) (* 182 197 136 2 242) (* 226 142 34 124) (max 4025 186042) (min 30059 126119002) (min 9 260 162) (* (< 4 4) 28699) (* 1945 (== 1714 1714)) (* 7 (< 1545 108)) (+ 12) (* 200 (> 31050 655605)) 3154 (* 3 (< 64896 116)) 3055 (* 13) (min 48082 226938 1175 68077774919) (+ 66 15 181 1380642642 11831587) (* 241 59) (* 150 (> 2742 113)) 37007908601 (max 52444 11 13008816 2935) 20723 8 (* 5 (> 6241732 759708)) (+ (* 15 7 4) (* 14 2 12) (* 13 6 6)) (+ 2877 229333 <tel:6558201020971|655820 1020971>) (+ 39581 2 14) (max 982557 44 31) 68 (* (== 11530 3492) 41177) (* (== 236 918711093) 3937) (max 903466 228 6 25989131 4028) 229 (min <tel:29987510969849|299875 10969849> 11481 2281 13) (* 55300721 (> 63 63)) (* 244 (> (+ 7 13 7) (+ 12 5 14))) (* 4494263 (== (+ 4 15 4) (+ 3 3 14))) (* (< 45 3307915) 58514) (* 3596530693 (< (+ 3 12 4) (+ 9 11 2))))
Michael de Kaste
12/16/2021, 10:10 AMephemient
12/16/2021, 10:20 AMphldavies
12/16/2021, 11:30 AMknthmn
12/16/2021, 11:31 AMDeepRecursiveFunction
, always amazes me when coroutine is used for something other than concurrency.ephemient
12/16/2021, 11:34 AMphldavies
12/16/2021, 11:54 AMJakub Gwóźdź
12/16/2021, 3:52 PMNir
12/16/2021, 5:53 PMMichael de Kaste
12/16/2021, 6:02 PMphldavies
12/16/2021, 6:05 PMNir
12/16/2021, 6:09 PMNir
12/16/2021, 6:10 PMsealed class Packet() {
abstract val version: Int
data class Literal(override val version: Int, val value: Long): Packet()
data class Operator(override val version: Int, val typeId: Int, val subpackets: List<Packet>): Packet()
}
Nir
12/16/2021, 6:10 PMNir
12/16/2021, 6:12 PMfun Packet.versionSum(): Long = version + if (this is Packet.Operator) subpackets.sumOf { it.versionSum() } else 0
Nir
12/16/2021, 6:12 PMfun Packet.evaluate(): Long {
if (this !is Packet.Operator) {
return (this as Packet.Literal).value
}
if (typeId in 5..7) {
assert(subpackets.size == 2)
}
val subValues = subpackets.map { it.evaluate() }
return when (typeId) {
0 -> subValues.sum()
1 -> subValues.fold(1L) { x, y -> x * y }
2 -> subValues.minOrNull()!!
3 -> subValues.maxOrNull()!!
5 -> if (subValues[0] > subValues[1]) 1 else 0
6 -> if (subValues[0] < subValues[1]) 1 else 0
7 -> if (subValues[0] == subValues[1]) 1 else 0
else -> throw RuntimeException("unreachable")
}
}
Nir
12/16/2021, 6:13 PMPacket
is incorrectMichael de Kaste
12/16/2021, 6:14 PMNir
12/16/2021, 6:14 PMMichael de Kaste
12/16/2021, 6:14 PMNir
12/16/2021, 6:15 PMDan Fingal-Surma
12/16/2021, 6:18 PMNir
12/16/2021, 6:20 PMDan Fingal-Surma
12/16/2021, 6:29 PMDan Fingal-Surma
12/16/2021, 6:29 PMfun Packet.render(): String = when (this) {
is Literal -> "$value"
is Operator -> {
val results = subpackets.map { it.render() }.joinToString(" ")
val op = when (typeId) {
0 -> "+"
1 -> "*"
2 -> "min"
3 -> "max"
5 -> ">"
6 -> "<"
7 -> "=="
else -> error("")
}
"($op $results)"
}
else -> error("")
}
Dan Fingal-Surma
12/16/2021, 6:30 PMephemient
12/16/2021, 6:30 PMDan Fingal-Surma
12/16/2021, 6:30 PMNir
12/16/2021, 6:30 PMDan Fingal-Surma
12/16/2021, 6:30 PMvalue.binaryToInt().toLong()
it isNir
12/16/2021, 6:30 PMDan Fingal-Surma
12/16/2021, 6:31 PMfun List<Char>.binaryToInt() = joinToString("").toInt(radix = 2)
fun List<Char>.binaryToLong() = joinToString("").toLong(radix = 2)
Nir
12/16/2021, 6:31 PMNir
12/16/2021, 6:31 PMfun List<Int>.binaryToInt() = reversed().mapIndexed { index, it -> it.toLong() * (1 shl index) }.sum()
Nir
12/16/2021, 6:31 PMDan Fingal-Surma
12/16/2021, 6:31 PMDan Fingal-Surma
12/16/2021, 6:31 PMephemient
12/16/2021, 6:31 PMfold(0L) { acc, x -> 2 * acc + x }
would be a be better way to do that anywayephemient
12/16/2021, 6:32 PMNir
12/16/2021, 6:32 PMNir
12/16/2021, 6:32 PMNir
12/16/2021, 6:32 PMInt
default everywhereNir
12/16/2021, 6:33 PMephemient
12/16/2021, 6:33 PMx shl i
has the same behavior as x * (1 shl i)
Nir
12/16/2021, 6:33 PMephemient
12/16/2021, 6:34 PMNir
12/16/2021, 6:34 PMDan Fingal-Surma
12/16/2021, 7:03 PMtoInt
on String
threw due to too much inputtodd.ginsberg
12/16/2021, 7:36 PMIterator<Char>
around, writing some extension functions to make it easier to work with, and defining a small sealed class hierarchy.
• Blog
• Codeephemient
12/16/2021, 7:44 PMCharIterator
and BooleanIterator
phldavies
12/16/2021, 8:04 PMIterator<Boolean>
(was going to extend BooleanIterator
but it's a class)
private interface Biterator : Iterator<Boolean> {
val remaining: Int
fun next(bits: Int): Int
override fun hasNext() = remaining > 0
override fun next() = next(1) == 1
}
phldavies
12/16/2021, 8:06 PMNir
12/16/2021, 8:09 PMphldavies
12/16/2021, 8:19 PM.substring(ofs, ofs + bits).toInt(2)
to consume from itMarcin Wisniowski
12/17/2021, 12:04 AMMarcin Wisniowski
12/17/2021, 12:06 AMDan Fingal-Surma
12/17/2021, 2:04 AMreduce(Long::times)
David Whittaker
12/17/2021, 2:26 AMreduce(Long::plus)
ephemient
12/17/2021, 2:41 AMsum()
is more like fold(0L, Long::plus)
because it works on empty listsNir
12/17/2021, 2:44 AMx.reduce { x, y -> x * y }
over reduce(Long::times)
David Whittaker
12/17/2021, 2:51 AMNir
12/17/2021, 3:09 AMephemient
12/17/2021, 3:23 AMLong::times
is less to type, but with Kotlin I doubt that is a limiting factorKiet
12/17/2021, 10:00 AM