-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy24day2.kt
More file actions
29 lines (25 loc) · 807 Bytes
/
Copy pathy24day2.kt
File metadata and controls
29 lines (25 loc) · 807 Bytes
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
import kotlin.math.abs
import kotlin.math.sign
fun main() {
val reports = generateSequence {
readlnOrNull()?.split(' ')?.map(String::toInt)
}.toList()
fun Sequence<Int>.isSafe(): Boolean =
windowed(2) { (a, b) -> a - b }.run {
val firstSign = first().sign
all { diff ->
diff.sign == firstSign && abs(diff) in 1..3
}
}
// part 1
reports.count { it.asSequence().isSafe() }.also(::println)
// part 2
fun <T> List<T>.subsequences() = sequence {
repeat(size) { iToSkip ->
yield(asSequence().filterIndexed { i, _ -> i != iToSkip })
}
}
reports.count { levels ->
levels.run { asSequence().isSafe() || subsequences().any { it.isSafe() } }
}.also(::println)
}