1
0
This commit is contained in:
Hubert Van De Walle 2020-12-09 06:45:26 +01:00
parent 1923e54548
commit 77dd1e2cc5
2 changed files with 1051 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Input
import be.vandewalleh.aoc.utils.input.Lines
import be.vandewalleh.aoc.utils.input.createDay
@Day(9)
class Day09(@Lines val input: Input<LongArray>) {
fun part1() = input.value
.toList()
.windowed(size = 26)
.find { !isValid(it) }
?.last()
private fun isValid(items: List<Long>): Boolean {
val preamble = items.take(25)
val last = items.last()
for (e in preamble) {
for (f in preamble) {
if (e + f == last && e != f) {
return true
}
}
}
return false
}
fun part2(): Long {
val a = 731031916L
var size = 2
while (true) {
for (startIndex in input.value.indices) {
val lastIndex = input.value.size - 1 - size
if (startIndex + size > lastIndex) break
val slice = input.value.sliceArray(startIndex..startIndex + size)
val sum = slice.reduce { acc, l -> acc + l }
if (sum == a) return slice.minOrNull()!! + slice.maxOrNull()!!
}
size++
}
}
}
fun main() = with(createDay<Day09>()) {
println(part1())
println(part2())
}

File diff suppressed because it is too large Load Diff