complete day 1
This commit is contained in:
commit
ddf95b2947
2 changed files with 46 additions and 0 deletions
22
src/01-a.kts
Normal file
22
src/01-a.kts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import java.util.Scanner
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
val scanner = Scanner(System.`in`)
|
||||||
|
|
||||||
|
val listLeft = ArrayList<Int>()
|
||||||
|
val listRight = ArrayList<Int>()
|
||||||
|
|
||||||
|
while (scanner.hasNextInt()) {
|
||||||
|
val num1 = scanner.nextInt()
|
||||||
|
listLeft.add(num1)
|
||||||
|
|
||||||
|
val num2 = scanner.nextInt()
|
||||||
|
listRight.add(num2)
|
||||||
|
}
|
||||||
|
|
||||||
|
val pairs = listLeft.sorted() zip listRight.sorted()
|
||||||
|
|
||||||
|
val result = pairs.fold(0) { acc, (l, r) -> acc + abs(l - r) }
|
||||||
|
|
||||||
|
println(result)
|
||||||
|
|
24
src/01-b.kts
Normal file
24
src/01-b.kts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import java.util.Scanner
|
||||||
|
|
||||||
|
val scanner = Scanner(System.`in`)
|
||||||
|
|
||||||
|
val listLeft = ArrayList<Int>()
|
||||||
|
val listRight = ArrayList<Int>()
|
||||||
|
|
||||||
|
while (scanner.hasNextInt()) {
|
||||||
|
val num1 = scanner.nextInt()
|
||||||
|
listLeft.add(num1)
|
||||||
|
|
||||||
|
val num2 = scanner.nextInt()
|
||||||
|
listRight.add(num2)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
val rightFrequencies = listRight.groupingBy { it }.eachCount()
|
||||||
|
|
||||||
|
val sum = listLeft.fold(0, { sum, left ->
|
||||||
|
sum + left * (rightFrequencies[left] ?: 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
println(sum)
|
||||||
|
|
Loading…
Add table
Reference in a new issue