Advent of Code 2023 day 11
12/11/2024, 5:00 AMDan Fingal-Surma
12/11/2024, 5:33 AMDan Fingal-Surma
12/11/2024, 5:33 AMDan Fingal-Surma
12/11/2024, 5:35 AMDan Fingal-Surma
12/11/2024, 5:36 AMMarcin Wisniowski
12/11/2024, 5:37 AMMarcin Wisniowski
12/11/2024, 5:37 AMPetr Sýkora
12/11/2024, 5:37 AMDan Fingal-Surma
12/11/2024, 5:39 AMPetr Sýkora
12/11/2024, 5:41 AMEl Anthony
12/11/2024, 5:48 AMDan Fingal-Surma
12/11/2024, 5:49 AMMarcin Wisniowski
12/11/2024, 5:50 AMOzioma Ogbe
12/11/2024, 5:54 AMJohnny Wulgaru
12/11/2024, 6:01 AMMemoizedDeepRecursiveFunction
but can't seem to get it to work as fast as my simple MutableMap cache 🤔Michael de Kaste
12/11/2024, 6:06 AMbj0
12/11/2024, 6:13 AMnkiesel
12/11/2024, 6:29 AMPetr Sýkora
12/11/2024, 6:33 AMbj0
12/11/2024, 6:33 AMDan Fingal-Surma
12/11/2024, 6:52 AMMap<Long, Int>
is a totally different, valid approach. That’s actually how the #1 ranked person did itDan Fingal-Surma
12/11/2024, 6:53 AMDan Fingal-Surma
12/11/2024, 6:56 AMLuan Tran
12/11/2024, 6:59 AMkingsley
12/11/2024, 7:00 AMRiccardo Lippolis
12/11/2024, 7:15 AMephemient
12/11/2024, 7:20 AMephemient
12/11/2024, 7:21 AMMap<Long, Long>
for that purpose ended up being all I neededritesh
12/11/2024, 7:37 AMDan Fingal-Surma
12/11/2024, 8:08 AMDan Fingal-Surma
12/11/2024, 8:35 AMritesh
12/11/2024, 8:45 AMNo matter how the stones change, their order is preserved, and they stay on their perfectly straight line.This tripped me for a bit. I assume, even if we shuffle numbers on every blink - the result should be same.
Dan Fingal-Surma
12/11/2024, 8:51 AMephemient
12/11/2024, 8:51 AMJaap Beetstra
12/11/2024, 8:55 AMLuan Tran
12/11/2024, 8:56 AMAnirudh
12/11/2024, 9:08 AMAnirudh
12/11/2024, 9:16 AMMichael Böiers
12/11/2024, 9:28 AMJakub Gwóźdź
12/11/2024, 9:59 AMprivate fun Long.blink(times: Long) = when (this) {
0L -> listOf(1L to times)
in (10..99) -> listOf(this / 10 to times, this % 10 to times)
in (1000..9999) -> listOf(this / 100 to times, this % 100 to times)
in (100000..999999) -> listOf(this / 1000 to times, this % 1000 to times)
in (10000000..99999999) -> listOf(this / 10000 to times, this % 10000 to times)
in (1000000000..9999999999) -> listOf(this / 100000 to times, this % 100000 to times)
in (100000000000..999999999999) -> listOf(this / 1000000 to times, this % 1000000 to times)
else -> listOf(this * 2024 to times)
}
xdAnirudh
12/11/2024, 10:02 AM(10L..99L)
and (1000L..9999L)
etc.Jakub Gwóźdź
12/11/2024, 10:02 AMPart 1 took 185.833us, result is 239714
Part 2 took 8.262500ms, result is 284973560658514
Jakub Gwóźdź
12/11/2024, 10:04 AMAnirudh
12/11/2024, 10:06 AMthis: Long
and never `Int`'s but I don't know what compile-time optimisations existJakub Gwóźdź
12/11/2024, 10:07 AMvalue.toIntExactOrNull()
Jakub Gwóźdź
12/11/2024, 10:07 AM@kotlin.jvm.JvmName("intRangeContains")
public operator fun ClosedRange<Int>.contains(value: Long): Boolean {
return value.toIntExactOrNull().let { if (it != null) contains(it) else false }
}
ephemient
12/11/2024, 10:56 AMin
range literals get optimized by the compiler anywayephemient
12/11/2024, 10:57 AMAnirudh
12/11/2024, 12:27 PMtoLong()
.
Jakub has confirmed that switching those to `L`'s did not affect performance - so the comparisons are probably being done between `Long`'s and no runtime castingJakub Gwóźdź
12/11/2024, 12:33 PMvar l = Random.nextLong()
val t = l in 40..50
gets compiled to
L0
LINENUMBER 6 L0
GETSTATIC kotlin/random/Random.Default : Lkotlin/random/Random$Default;
INVOKEVIRTUAL kotlin/random/Random$Default.nextLong ()J
LSTORE 0
L1
LINENUMBER 7 L1
LDC 40
LLOAD 0
LCMP
IFGT L2
LLOAD 0
LDC 51
LCMP
...
I assume LCMP is a long compare. And 40
and 50
, being literals, are not casted in any way
Bytecode is the same for 40L..50L
Anirudh
12/11/2024, 12:38 PMLuan Tran
12/11/2024, 12:49 PMwhen(subject)
, there is no warning. But when I use when{}
, the IDE will have this warningritesh
12/11/2024, 12:52 PMLuan Tran
12/11/2024, 12:59 PMJakub Gwóźdź
12/11/2024, 1:03 PMLuan Tran
12/11/2024, 1:20 PMwhen
to get it right. Could there be a number that will eventually exceed Long?Jakub Gwóźdź
12/11/2024, 1:21 PMAnirudh
12/11/2024, 1:24 PM9,223,372,036,854,775,807
and Jakub's last range is:
999,999,999,999
so yes, it's possible you need 4 more entries (7 more digits)
as in, you probably don't exceed Long.MAX but need more entires in the when
Luan Tran
12/11/2024, 1:25 PM99,999,999,999,999
is enough. But I'm interested in what is the maximal digit reachable by the rule of the game.Anirudh
12/11/2024, 1:34 PMLuan Tran
12/11/2024, 1:57 PMphldavies
12/11/2024, 2:49 PMMemoizedDeepRecursiveFunction
has terrible performance for large caches as it uses an atomic<Map<K, V>
under the hood to ensure thread safety. I have the following for AoC usage:
@Suppress("FunctionName")
fun <T, R> CachedDeepRecursiveFunction(block: suspend DeepRecursiveScope<T, R>.(T) -> R) =
mutableMapOf<T, R>().run { DeepRecursiveFunction { getOrPut(it) { block(it) } } }
Jakub Gwóźdź
12/11/2024, 3:03 PM#REF
problem after only 23 blinks 😆Jaap Beetstra
12/11/2024, 3:07 PMJaap Beetstra
12/11/2024, 3:18 PMPaul Woitaschek
12/11/2024, 7:17 PMphldavies
12/11/2024, 9:21 PMWarming up 2 puzzles for 5s each for year 2024 day 11...
CountUnique warmed up with 960 iterations
Default warmed up with 581 iterations
year 2024 day 11 part 1
Default took 95.143us 👑: 207683
CountUnique took 118.958us (1.25x): 207683
year 2024 day 11 part 2
CountUnique took 4.783702ms 👑: 244782991106220
Default took 8.291658ms (1.73x): 244782991106220
kqr
12/11/2024, 10:54 PMephemient
12/11/2024, 10:56 PMkqr
12/11/2024, 10:56 PMkqr
12/11/2024, 10:57 PMkqr
12/11/2024, 10:57 PMkqr
12/11/2024, 10:58 PMkqr
12/11/2024, 11:00 PMNeil Banman
12/12/2024, 2:43 AMVitali Plagov
12/16/2024, 10:23 AM