-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay09EncodingError.kt
32 lines (26 loc) · 1.03 KB
/
Day09EncodingError.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
package adventofcode.year2020
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.cartesianProduct
class Day09EncodingError(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val numbers by lazy { input.lines().map(String::toLong) }
override fun partOne() =
(PREAMBLE_LENGTH until numbers.size)
.filter { index ->
val preamble = numbers.subList(index - PREAMBLE_LENGTH, index)
listOf(preamble, preamble).cartesianProduct().none { it.sum() == numbers[index] }
}
.map(numbers::get)
.first()
override fun partTwo(): Long {
val invalidNumber = partOne()
return (2..numbers.size)
.flatMap { size -> (0..numbers.size - size).map { numbers.subList(it, it + size) } }
.filter { it.sum() == invalidNumber }
.map { it.minOrNull()!! + it.maxOrNull()!! }
.first()
}
companion object {
private const val PREAMBLE_LENGTH = 25
}
}