aoc_2024/day01.kts

28 lines
902 B
Plaintext
Raw Normal View History

2024-12-02 13:06:26 +00:00
import java.io.File
import java.util.Scanner
import kotlin.math.abs
val scanner = Scanner(File("puzzle1input.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]
}
2024-12-02 13:06:27 +00:00
println("Total distance: $totaldistance")
println("Similarity score: $similarityscore")