Advent of Code 2022 day 20
12/20/2023, 5:00 AMMarcin Wisniowski
12/20/2023, 6:28 AMJonathan Kolberg
12/20/2023, 6:28 AMVitaly Legchilkin
12/20/2023, 7:10 AMbj0
12/20/2023, 7:13 AMJonathan Kolberg
12/20/2023, 7:13 AMbj0
12/20/2023, 7:14 AMVitaly Legchilkin
12/20/2023, 7:16 AMJonathan Kolberg
12/20/2023, 7:18 AMVitaly Legchilkin
12/20/2023, 7:19 AMVitaly Legchilkin
12/20/2023, 7:21 AMMichael de Kaste
12/20/2023, 7:27 AMJonathan Kolberg
12/20/2023, 7:28 AMMichael de Kaste
12/20/2023, 7:44 AMMichael de Kaste
12/20/2023, 7:44 AMJonathan Kolberg
12/20/2023, 8:07 AMJonathan Kolberg
12/20/2023, 8:18 AMPawel Matysek
12/20/2023, 9:07 AMkingsley
12/20/2023, 9:19 AMWerner Altewischer
12/20/2023, 10:47 AM/**
* Computes the greatest common divisor of two integers.
*/
tailrec fun greatestCommonDivisor(a: Long, b: Long): Long {
if (b == 0L) {
return a
}
return greatestCommonDivisor(b, a % b)
}
/**
* Computes the least common multiple of two integers.
*/
fun leastCommonMultiple(a: Long, b: Long): Long {
return if (a == 0L || b == 0L) 0L else {
val gcd = greatestCommonDivisor(a, b)
abs(a * b) / gcd
}
}
/**
* Takes a list of base/modulo combinations and returns the lowest number for which the states coincide such that:
*
* for all i: state(i) == base_state(i).
*
* E.g. chineseRemainder((3,4), (5,6), (2,5)) == 47
*/
fun chineseRemainder(values: List<Pair<Long, Long>>): Long {
if (values.isEmpty()) {
return 0L
}
var result = values[0].first
var lcm = values[0].second
for (i in 1 until values.size) {
val (base, modulo) = values[i]
val target = base % modulo
while (result % modulo != target) {
result += lcm
}
lcm = leastCommonMultiple(lcm, modulo)
}
return result
}
ephemient
12/20/2023, 12:12 PMephemient
12/20/2023, 12:13 PMreturn null
in case any of the underlying assumptions are invalidMichael de Kaste
12/20/2023, 12:15 PMPaul Woitaschek
12/20/2023, 12:19 PMephemient
12/20/2023, 12:20 PMsed -e '1idigraph {' -e 's/%\(\w\+\)/\1[shape=box];\1/' -e 's/&\(\w\+\)/\1[shape=diamond];\1/' -e 's/$/;/' -e '$a}' input.txt | dot -Tsvg -oday20.svg
Werner Altewischer
12/20/2023, 2:06 PMMichael de Kaste
12/20/2023, 2:08 PMMarcin Wisniowski
12/20/2023, 2:10 PMMax Thiele
12/20/2023, 2:21 PMephemient
12/20/2023, 2:45 PMMichael de Kaste
12/20/2023, 2:56 PMJohannes Gätjen
12/20/2023, 2:56 PMMichael de Kaste
12/20/2023, 2:56 PMMichael de Kaste
12/20/2023, 2:58 PMWerner Altewischer
12/20/2023, 2:59 PMJonathan Kolberg
12/20/2023, 2:59 PMWerner Altewischer
12/20/2023, 3:00 PMephemient
12/20/2023, 3:00 PMMichael de Kaste
12/20/2023, 3:01 PMephemient
12/20/2023, 3:03 PMNeil Banman
12/20/2023, 4:43 PMNeil Banman
12/20/2023, 5:03 PMMax Thiele
12/20/2023, 5:06 PMcoding it up Java OOP-style, with interfaces, dummy functions that don't do anything, stateful objects with a reset function, etc. 🙂I'm guilty on that one too 😅
phldavies
12/20/2023, 6:12 PMphldavies
12/20/2023, 6:13 PMKash Kabeya
12/20/2023, 6:29 PMphldavies
12/20/2023, 6:33 PMMichael de Kaste
12/20/2023, 6:37 PMKash Kabeya
12/20/2023, 6:38 PMhigh
. I recorded the 5 entries for each inputs and the increase was linear. I am guessing that was the intention 🤷🏾♂️Neil Banman
12/20/2023, 11:03 PMoverride fun part2(): Long {
val flipFlops = Module.lookup.values.filterIsInstance<FlipFlop>().map { it.name }.toSet()
val binaryCounterResults = Module.lookup.getValue("broadcaster").downstream
.map { name ->
val start = Module.lookup.getValue(name)
generateSequence(start) { module -> Module.lookup[module.downstream.firstOrNull { it in flipFlops }] }
.map { if (Module.upstreamCount.getValue(it.name) == 1) 1 else 0 }
.foldIndexed(1L) { index, acc, i -> acc + (i shl index) }
}
return lcm(binaryCounterResults)
}