2024 day 1

This commit is contained in:
Sven Weidauer 2024-12-01 11:54:58 +01:00
parent 1db15c4581
commit 7d8dc6bc82
13 changed files with 1140 additions and 0 deletions

View file

@ -0,0 +1,40 @@
import kotlin.math.abs
fun main() {
val input = object {}.javaClass.getResourceAsStream("day1.txt")
?.bufferedReader()
?.lineSequence()
?: error("Cannot read input")
val a = mutableListOf<Int>()
val b = mutableListOf<Int>()
val spaces = "\\s+".toRegex()
for (line in input) {
val parts = line.split(spaces, limit = 2)
val (first, second) = parts
a.add(first.toInt())
b.add(second.toInt())
}
assert(a.size == b.size)
a.sort()
b.sort()
var sum = 0
for (index in a.indices) {
sum += abs(a[index] - b[index])
}
println("Part 1: $sum")
sum = 0
for (value in a) {
sum += value * b.count { it == value }
}
println("Part 2: $sum")
}

File diff suppressed because it is too large Load diff