generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay07.kt
46 lines (36 loc) · 1.16 KB
/
Day07.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package year2021.`07`
import readInput
import kotlin.math.abs
fun main() {
fun calculateFullAmountOfFuelToPosition(
position: Int,
map: Map<Int, Int>,
modifier: (Int) -> Int
): Int {
return map.keys.sumOf {
modifier(abs(it - position)) * map[it]!!
}
}
fun solution(
input: List<String>,
modifier: (Int) -> Int
): Int {
val mappedResult = input.first()
.split(",")
.map { it.toInt() }
.groupBy { it }
.mapValues { it.value.count() }
return (mappedResult.keys.min()..mappedResult.keys.max())
.minOf { calculateFullAmountOfFuelToPosition(it, mappedResult, modifier) }
}
fun part1(input: List<String>): Int = solution(input) { n -> n }
fun part2(input: List<String>): Int = solution(input) { n -> n * (n + 1) / 2 }
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day07_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 37)
val input = readInput("Day07")
println(part1(input))
println(part2(input))
}