Day09
This commit is contained in:
parent
1923e54548
commit
77dd1e2cc5
51
days/src/main/kotlin/Day09.kt
Normal file
51
days/src/main/kotlin/Day09.kt
Normal 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())
|
||||
}
|
||||
1000
days/src/main/resources/day09.txt
Normal file
1000
days/src/main/resources/day09.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user