Advent of Code 2023 day 1
12/01/2024, 5:00 AMMarcin Wisniowski
12/01/2024, 5:14 AMMarcin Wisniowski
12/01/2024, 5:14 AMMarcin Wisniowski
12/01/2024, 5:15 AMNeil Banman
12/01/2024, 5:18 AMval (a, b) = input.getIntList().collate(2)
Michael de Kaste
12/01/2024, 5:24 AMNeil Banman
12/01/2024, 5:25 AMKroppeb
12/01/2024, 5:29 AMNeil Banman
12/01/2024, 5:31 AMKroppeb
12/01/2024, 5:31 AMKroppeb
12/01/2024, 5:32 AMNeil Banman
12/01/2024, 5:32 AMMarcin Wisniowski
12/01/2024, 5:32 AMsleep(200)
. If your process finishes immediately after trying to put something into the clipboard, it doesn't apply.Michael de Kaste
12/01/2024, 5:32 AMKroppeb
12/01/2024, 5:36 AMMarcin Wisniowski
12/01/2024, 5:37 AMints()
and getIntList()
helpers you all have, I'll have to borrow the idea.bj0
12/01/2024, 5:39 AMMichael de Kaste
12/01/2024, 5:46 AMbj0
12/01/2024, 5:46 AMunzip
bj0
12/01/2024, 6:04 AMAnirudh
12/01/2024, 6:13 AM.getOrPut()
so that counting does not repeat for each repeated number in the left list; since the repeated number would occur the same number of times in the right list.Norbert Kiesel
12/01/2024, 6:13 AMfun String.ints() = Regex("""-?\d+""").findAll(this).map { it.value.toInt() }.toList()
fun String.longs() = Regex("""-?\d+""").findAll(this).map { it.value.toLong() }.toList()
Jakub Gwóźdź
12/01/2024, 6:15 AMprivate fun List<Long>.frequencies(): Map<Long, Int> =
buildMap<Long, Int> { this@frequencies.forEach { compute(it) { _, u -> (u ?: 0) + 1 } } }
but anyway, it's a nice warmup challenge, just to get re-acquainted with stdlibs.
Could be as well done in a spreadsheet 🙂PoisonedYouth
12/01/2024, 7:08 AMzip
function 👌Jakub Gwóźdź
12/01/2024, 7:34 AMAnirudh
12/01/2024, 7:38 AMI used a mutable map withI wonder if twoso that counting does not repeat.getOrPut()
getEachCount
(not nested) would be better.
so then sum of x * rightListCount[x] * leftListCount[x]
Jakub Gwóźdź
12/01/2024, 7:40 AMJakub Gwóźdź
12/01/2024, 7:41 AMJakub Gwóźdź
12/01/2024, 7:41 AMJakub Gwóźdź
12/01/2024, 7:42 AMAnirudh
12/01/2024, 7:44 AM(35412, 20), (26729, 20), (88579, 19), (89301, 19)
but only about 30 items have any repeats so maybe pre-counting doesn't save much.
and with just 1,000 items in the actual input; these optimisations won't save muchJakub Gwóźdź
12/01/2024, 7:59 AMAnirudh
12/01/2024, 8:01 AMDan Fingal-Surma
12/01/2024, 8:09 AMJakub Gwóźdź
12/01/2024, 8:12 AMprivate fun List<Long>.frequencies(): Map<Long, Int> = buildList<Pair<Long, Int>> {
this@frequencies.forEach { l ->
if (isNotEmpty() && last().first == l) add(l to removeLast().second + 1)
else add(l to 1)
}
}.toMap()
Dan Fingal-Surma
12/01/2024, 8:12 AMval frequencies = secondList.groupBy { it }.mapValues { it.value.size }
Marcin Wisniowski
12/01/2024, 8:21 AMsince we have the right list already sorted from the part1Well, depending on your approach. I keep my part1 and part2 solutions separate, they have nothing in common. Makes the part 2 solution cleaner since the code doesn't need to solve part 1 anymore. But that results in more code overall, of course, since similarities end up duplicated across the two solutions.
phldavies
12/01/2024, 8:23 AMJakub Gwóźdź
12/01/2024, 8:23 AMJakub Gwóźdź
12/01/2024, 8:36 AMNeil Banman
12/01/2024, 9:25 AMTim Schraepen
12/01/2024, 10:00 AMKarloti
12/01/2024, 12:43 PMfun main() {
val s1 = mutableListOf<Int>() // List to store numbers from the first column
val s2 = mutableListOf<Int>() // List to store numbers from the second column
fun load(fileName: String) {
s1.clear()
s2.clear()
readInput(fileName).map { line ->
line.split(Regex("\\s+")) // Split the line by whitespace
.map(String::toInt) // Convert strings to integers
.let { (e1, e2) -> s1.add(e1); s2.add(e2) } // Add to respective lists
}
}
fun part1(): Int {
s1.sort() // Sort the first list
s2.sort() // Sort the second list
return (s1 zip s2).sumOf { (a, b) -> (a - b).absoluteValue }
}
fun part2(): Int {
val map = s2.groupingBy { it }.eachCount()
return s1.sumOf { it * (map[it] ?: 0) }
}
// Load input for the test case and verify the results
load("Day01_test")
part1().let { result ->
println("result1 = $result")
check(result == 11) { "part1 Day01_test" }
}
part2().let { result ->
println("result2 = $result")
check(result == 31) { "part2 Day01_test" }
}
// Load the actual input and solve the puzzle
load("Day01")
part1().println()
part2().println()
}
Karloti
12/01/2024, 12:48 PMPaul Woitaschek
12/01/2024, 1:04 PMKarloti
12/01/2024, 1:04 PMJakub Gwóźdź
12/01/2024, 1:17 PMleft
and right
are sorted (and left is unique, as in puzzle input)
fun part1(left: List<Long>, right: List<Long>): Long = left.asSequence()
.zip(right.asSequence()) { l, r -> abs(r - l) }
.sum()
fun part2(left: List<Long>, right: List<Long>): Long = left.fold(0 to 0L) { a, l ->
var (j, s) = a
while (j < right.size && right[j] < l) j++
while (j < right.size && right[j] == l) j++.also { s = s + l }
j to s
}.second
todd.ginsberg
12/01/2024, 3:54 PMPoisonedYouth
12/01/2024, 4:56 PMPaul Woitaschek
12/01/2024, 7:19 PMDan Fingal-Surma
12/01/2024, 11:07 PMsecondList.groupingBy { it }.eachCount()
better than secondList.groupBy { it }.mapValues { it.value.size }
Kroppeb
12/02/2024, 4:33 AMtodd.ginsberg
12/02/2024, 1:57 PMzipWithNext
which is always a good time. Brute forced the second part because it's day 2 and we can get away with things like that.
I lost a bit of time (not that I'm trying for the leaderboard by any stretch) because for the actual input, I was running it against my Day 1 class (yay cut and paste errors) and it was actually giving a result, haha. Wrong result, but still. Kinda funny looking back at it.
* Code
* Blogephemient
12/03/2024, 2:47 PMManel Martos
12/16/2024, 10:10 PMJakub Gwóźdź
12/16/2024, 10:33 PM