Advent of Code 2023 day 5
12/05/2024, 5:00 AMJonathan Kolberg
12/05/2024, 5:25 AMlist.size/2
as index
https://github.com/bulldog98/advent-of-code/blob/main/advent2024/src/main/kotlin/year2024/Day05.ktMarcin Wisniowski
12/05/2024, 5:37 AMMarcin Wisniowski
12/05/2024, 5:38 AMy9san9
12/05/2024, 5:43 AMMarcin Wisniowski
12/05/2024, 5:43 AMy9san9
12/05/2024, 5:48 AMMarcin Wisniowski
12/05/2024, 5:49 AMfor (i in 0..(new.size / 2)) {
and avoid the useless sorting of the second half of the list. 😄Marcin Wisniowski
12/05/2024, 5:54 AMy9san9
12/05/2024, 5:55 AMMichael de Kaste
12/05/2024, 6:01 AMRenette Ros
12/05/2024, 6:08 AMbj0
12/05/2024, 6:15 AMbj0
12/05/2024, 6:20 AMbj0
12/05/2024, 6:46 AMJakub Gwóźdź
12/05/2024, 6:46 AMJakub Gwóźdź
12/05/2024, 6:48 AMbuildMap<String, MutableSet<String>>
, otherwise it's immutable soon after it leaves the lambda in getOrPutbj0
12/05/2024, 6:48 AMJakub Gwóźdź
12/05/2024, 6:48 AMJakub Gwóźdź
12/05/2024, 6:48 AMMichael de Kaste
12/05/2024, 6:49 AMLidonis Calhau
12/05/2024, 6:58 AMisSortedWith(comparator)
function so I wrote it myself as extension but I feel like I might have missed something.
What do you think ?Michael de Kaste
12/05/2024, 7:01 AMLidonis Calhau
12/05/2024, 7:08 AMfun <T> List<T>.isSortedWith(comparator: Comparator<T>): Boolean {
return asSequence().zipWithNext().all { comparator.compare(it.first, it.second) <= 0 }
}
phldavies
12/05/2024, 7:39 AMAnirudh
12/05/2024, 7:45 AMbeforePages
was already defined for part 1, my part 2 comparator was:
private val pageComparator = Comparator<Int> { a, b ->
when {
b in beforePages[a].orEmpty() -> -1
a in beforePages[b].orEmpty() -> 1
else -> 0
}
}
Anirudh
12/05/2024, 7:47 AMbeforePages.getOrDefault(page, emptySet())
can be replaced with:
beforePages[page].orEmpty()
Endre Deak
12/05/2024, 7:47 AMsolve("Print Queue") {
val (rules, queue) = text
.split("\n\n")
.let { it[0].split("\n") to it[1].split("\n") }
.let { (f, s) -> f.map { it.ints() }.groupBy({ it[0] }) { it[1] } to s.map { it.ints() } }
val comparator = Comparator<Int> { l, r -> rules[l]?.let { if (r in it) -1 else 1 } ?: 0 }
part1() {
queue
.filter { it == it.sortedWith(comparator) }
.sumOf { it.middle() }
}
part2() {
queue
.filter { it != it.sortedWith(comparator) }
.map { it.sortedWith(comparator) }
.sumOf { it.middle() }
}
}
Dan Fingal-Surma
12/05/2024, 7:58 AMDan Fingal-Surma
12/05/2024, 8:03 AMimposes a _total ordering_
phldavies
12/05/2024, 8:05 AMComparator<T>.isSorted
fun.
Warming up 2 puzzles for 5s each for year 2024 day 5...
Default warmed up with 635 iterations
Map warmed up with 9540 iterations
year 2024 day 5 part 1
Map took 219.608us (1.00x): 4790
Default took 3.561533ms (16.22x): 4790
year 2024 day 5 part 2
Map took 345.216us (1.00x): 6319
Default took 3.397258ms (9.84x): 6319
slightly improved performanceDan Fingal-Surma
12/05/2024, 8:06 AMphldavies
12/05/2024, 8:07 AMcompare(a, b) === -compare(b, a)
is a requirement of a comparatorDan Fingal-Surma
12/05/2024, 8:08 AMcompare(a, b) == 0
, compare(a, c) == 0
, but compare(b, c) == 1
. In general it warns about returning 0 inconsistent with equals.Michael de Kaste
12/05/2024, 8:11 AMAnirudh
12/05/2024, 8:12 AMDan Fingal-Surma
12/05/2024, 8:13 AMAnirudh
12/05/2024, 8:15 AMreversed(sorted(lst)) == sorted(lst, reverse=True)
Paul Woitaschek
12/05/2024, 8:36 AMJan Durovec
12/05/2024, 8:46 AMDan Fingal-Surma
12/05/2024, 9:14 AMAnirudh
12/05/2024, 9:15 AMMax Thiele
12/05/2024, 10:23 AMritesh
12/05/2024, 10:28 AMJakub Gwóźdź
12/05/2024, 10:53 AMval ordering = lines.takeWhile(String::isNotBlank).map { it.split("|").let { (a, b) -> a.toLong() to b.toLong() } }.toSet()
fun <T> comparator(ordering: Set<Pair<T, T>>) = Comparator<T> { o1, o2 ->
when {
(o1 to o2) in ordering -> -1
(o2 to o1) in ordering -> 1
else -> 0
}
}
phldavies
12/05/2024, 11:16 AMJakub Gwóźdź
12/05/2024, 11:19 AMp = o1.shl(4)+o2
if creating a pair be a major problem 🙂phldavies
12/05/2024, 11:51 AMWarming up 3 puzzles for 5s each for year 2024 day 5...
BitSet warmed up with 13237 iterations
Default warmed up with 617 iterations
Map warmed up with 10242 iterations
year 2024 day 5 part 1
BitSet took 168.2us 👑: 4790
Map took 207.94us (1.24x): 4790
Default took 4.067499ms (24.18x): 4790
year 2024 day 5 part 2
BitSet took 189.803us 👑: 6319
Map took 280.196us (1.48x): 6319
Default took 4.067117ms (21.43x): 6319
with comparator:
infix fun Int.merge(other: Int): Int = or(other shl 7)
fun BitSet.asComparator(): Comparator<Int> = Comparator { o1, o2 ->
when {
get(o1 merge o2) -> -1
get(o2 merge o1) -> 1
else -> 0
}
}
val comparator = BitSet().apply {
for (rule in rules) {
rule.splitInts('|')
.also { (a, b) -> set(a merge b) }
}
}.asComparator()
Sergei Petunin
12/05/2024, 12:00 PMclass Day05 {
fun part1(input: String) = solve(input, true)
fun part2(input: String) = solve(input, false)
fun solve(input: String, correct: Boolean): Long {
val (ordering, updates) = input.split("\n\n")
val order = ordering.lines().toSet()
return updates.lines()
.map { it.split(",") }
.map { it to it.sortedWith { o1, o2 -> if ("$o1|$o2" in order) -1 else if ("$o2|$o1" in order) 1 else 0 } }
.filter { (it.first == it.second) == correct }
.sumOf { it.second[it.second.size / 2].toLong() }
}
}
ephemient
12/05/2024, 12:09 PMephemient
12/05/2024, 12:10 PMphldavies
12/05/2024, 12:17 PMSergei Petunin
12/05/2024, 12:29 PMundeadpotato
12/05/2024, 1:22 PMNeil Banman
12/05/2024, 4:28 PMcedrick
12/05/2024, 8:05 PMval rules = file.readLines()
.filter { it.contains("|") }
.map { it.split("|") }
.map { it[0].toLong() to it[1].toLong() }
.groupBy({ it.first }, { it.second })
//
//Then for each incorrectly-ordered updates
//
var importantRule = rules
.filter { rule -> update.any { rule.key == it } }
.mapValues { it.value.filter { value -> update.any { value == it } } }
.filter { it.value.isNotEmpty() }
val newUpdate = mutableListOf<Long>()
newUpdate.add(0, update.first { it !in importantRule.keys })
while (true) {
importantRule = importantRule.filter { it.key != newUpdate.first() }
.mapValues { it.value.filter { it != newUpdate.first() } }
newUpdate.add(0, importantRule.filter { it.value.isEmpty() }.keys.first())
if (newUpdate.size == update.size) break
}
Brent Berghmans
12/06/2024, 12:33 AMBrent Berghmans
12/06/2024, 12:33 AMBrent Berghmans
12/06/2024, 12:34 AMBrent Berghmans
12/06/2024, 12:46 AM