2024 day 1
This commit is contained in:
parent
1db15c4581
commit
7d8dc6bc82
13 changed files with 1140 additions and 0 deletions
40
2024/src/main/kotlin/day1.kt
Normal file
40
2024/src/main/kotlin/day1.kt
Normal 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")
|
||||
}
|
1000
2024/src/main/resources/day1.txt
Normal file
1000
2024/src/main/resources/day1.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue