generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.kt
33 lines (26 loc) · 866 Bytes
/
Task.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
package day01
import readInput
fun main() {
val input = readInput("day01")
println(solvePartOne(input))
println(solvePartTwo(input))
}
fun solvePartOne(input: List<String>): Int =
input.map { value -> value.toInt() }
.solveIncreasingTimes()
fun solvePartTwo(input: List<String>): Int = input.map { value -> value.toInt() }
.windowed(size = 3, step = 1, partialWindows = false) { list ->
list.sum()
}
.solveIncreasingTimes()
internal fun Iterable<Int>.solveIncreasingTimes(): Int = fold(Result()) { acc, value ->
val newCounter = when {
acc.previousHighest != null && value > acc.previousHighest -> acc.counter + 1
else -> acc.counter
}
acc.copy(previousHighest = value, counter = newCounter)
}.counter
data class Result(
val previousHighest: Int? = null,
val counter: Int = 0,
)