aoc_2024/day01.kts
Logiar 07e25fadd1
Some checks failed
Solve / example-action (push) Failing after 8s
Added extremely simple tests for future refactoring.
2024-12-03 08:26:53 +01:00

29 lines
922 B
Kotlin
Executable File

#!/usr/bin/env kotlin
import java.io.File
import java.util.Scanner
import kotlin.math.abs
val scanner = Scanner(File("day01input.txt"))
val firstList = mutableListOf<Int>()
val secondList = mutableListOf<Int>()
var secondMap = mutableMapOf<Int, Int>()
while (scanner.hasNextLine()) {
val line = scanner.nextLine()
val numbers = line.split(" ")
val firstNumber = numbers[0].toInt()
val secondNumber = numbers[1].toInt()
firstList.add(firstNumber)
secondList.add(secondNumber)
secondMap[secondNumber] = (secondMap[secondNumber] ?: 0) + 1
}
firstList.sort()
secondList.sort()
var totaldistance = 0
var similarityscore = 0
for (i in firstList.indices) {
val distance = abs(firstList[i] - secondList[i])
totaldistance += distance
similarityscore += (secondMap[firstList[i]] ?: 0) * firstList[i]
}
println("Total distance: $totaldistance")
println("Similarity score: $similarityscore")