Advent of Code 2021 day 14
12/14/2021, 5:00 AMDavid Whittaker
12/14/2021, 5:59 AMMichael de Kaste
12/14/2021, 6:02 AMjoinToString{""}
instead of joinToString("")
Dan Fingal-Surma
12/14/2021, 6:14 AMJakub Gwóźdź
12/14/2021, 6:24 AMDavid Whittaker
12/14/2021, 6:33 AMmap.value += 5
since the value might be null so I ended up doing map.value = (map.value ?: 0) + 5
. That seems tedious.David Whittaker
12/14/2021, 6:33 AMrules.forEach { rule ->
pairs.forEach { pair ->
if (pair.value > 0 && rule.first == pair.key) {
newPairs[pair.key] = (newPairs[pair.key] ?: 0) - pair.value
newPairs[pair.key[0] + rule.second] = (newPairs[pair.key[0] + rule.second] ?: 0) + pair.value
newPairs[rule.second + pair.key[1]] = (newPairs[rule.second + pair.key[1]] ?: 0) + pair.value
counts[rule.second[0]] = (counts[rule.second[0]] ?: 0) + pair.value
}
}
}
ephemient
12/14/2021, 6:38 AMephemient
12/14/2021, 6:40 AMmap = .withDefault { 0 }; map[value] = map.getValue(value) + 5
Michael Böiers
12/14/2021, 6:49 AMDan Fingal-Surma
12/14/2021, 6:56 AMDan Fingal-Surma
12/14/2021, 7:06 AMDan Fingal-Surma
12/14/2021, 7:11 AMfun <T> MutableMap<T, Long>.increment(key: T, amount: Long) =
compute(key) { _, v -> (v ?: 0L) + amount }
elizarov
12/14/2021, 7:21 AMgroupingBy
shines here. No need to do manual incrementing to count and sum up occurrences.David Whittaker
12/14/2021, 7:40 AMgroupingBy
just yetephemient
12/14/2021, 7:45 AM.eachCount()
Dan Fingal-Surma
12/14/2021, 7:52 AMfun <T> Iterable<T>.histogram(): Map<T, Long> = groupingBy { it }.eachCount().mapValues { it.value.toLong() }
elizarov
12/14/2021, 8:11 AMMichael de Kaste
12/14/2021, 8:12 AMJakub Gwóźdź
12/14/2021, 9:00 AMMichael Böiers
12/14/2021, 9:03 AMelizarov
12/14/2021, 9:03 AMnkiesel
12/14/2021, 9:03 AMclass CountingMap<T>(
l: List<T> = emptyList(),
private val m: MutableMap<T, Long> = mutableMapOf()
) : MutableMap<T, Long> by m {
init {
l.forEach { inc(it) }
}
fun inc(k: T, amount: Long = 1L) {
m[k] = (m[k] ?: 0L) + amount
}
}
Michael Böiers
12/14/2021, 9:03 AMfun <K> Iterable<K>.frequencies() = mutableMapOf<K, Long>().apply { this@frequencies.forEach { inc(it, 1) } }
fun <K> MutableMap<K, Long>.inc(k: K, n: Long) = set(k, get(k)?.let { it + n } ?: n)
elizarov
12/14/2021, 9:04 AM.eachCount().mapValues { it.toLong() }
Michael Böiers
12/14/2021, 9:04 AMelizarov
12/14/2021, 9:04 AMfrequencies() == groupingBy { it }.eachCount().mapValues { it.toLong() }
Michael de Kaste
12/14/2021, 9:04 AMMichael de Kaste
12/14/2021, 9:05 AMnkiesel
12/14/2021, 9:05 AMval parts = CountingMap(template.windowed(2, partialWindows = false))
Michael Böiers
12/14/2021, 9:05 AMelizarov
12/14/2021, 9:05 AMfold
elizarov
12/14/2021, 9:06 AMeachCount
only in the initialization step.Michael Böiers
12/14/2021, 9:07 AMgroupingBy { it }.eachCount()
.elizarov
12/14/2021, 9:10 AMgroupingB { it }.eachCount()
is really explicit and reads well. But it does happen a from time to time in real-life Kotlin code. It might make sense to have a shorter frequencies
in stdlib. However, my quick search still shows that it is needed in puzzles more often that in real-life…ephemient
12/14/2021, 9:14 AMGrouping.eachCount()
should rarely overflow an `Int`; collection sizes can't be larger than Int
to begin with. but a hypothetical Grouping.sumOf()
should have Long
overloadMichael Böiers
12/14/2021, 9:24 AMelizarov
12/14/2021, 9:29 AMrandom()
was primarily for testing, not for puzzles. The rule of thumb is that in order to be added to stdlib the function must be domain-agnostic (useful across variety of domains). And even that is not enough. It has to be either universally/widely needed or if it is needed occasionally/rarely, then it could because it is otherwise tricky/error-prone to implement (or because it could be non-trivial to implement efficiently).elizarov
12/14/2021, 9:31 AMephemient
12/14/2021, 9:34 AMMichael de Kaste
12/14/2021, 9:36 AMMichael de Kaste
12/14/2021, 9:38 AMephemient
12/14/2021, 9:41 AMMichael Böiers
12/14/2021, 10:14 AMkqr
12/14/2021, 12:09 PMJakub Gwóźdź
12/14/2021, 12:27 PMMichael de Kaste
12/14/2021, 12:28 PMMichael Böiers
12/14/2021, 12:32 PMphldavies
12/14/2021, 6:31 PMfun <T, K> Grouping<T, K>.eachSum(transform: (T) -> Long): Map<K, Long>
extension. I also needed an extension to merge two maps with conflict handling, but I wasn't sure if I'd missed something in stdlib.ephemient
12/14/2021, 6:41 PMmerge()
default method which allows for handling conflictsMarcin Wisniowski
12/14/2021, 7:06 PMtodd.ginsberg
12/14/2021, 7:25 PMgroupingBy { ... }.eachCount
. I agree, I only ever use it for puzzles and like it because it’s easy to read. I’d welcome a Iterable<T>.permutations()
in the stdlib, but I doubt it would pass the “mostly not used by puzzles only” rule.ephemient
12/14/2021, 7:29 PMtodd.ginsberg
12/14/2021, 7:30 PMKiet
12/15/2021, 7:07 AMKiet
12/15/2021, 7:07 AMtodd.ginsberg
12/15/2021, 8:02 PM