Created
December 9, 2020 06:02
-
-
Save saidaspen/1c505d416c0be3b6df4177ca4210ca71 to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day 9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun part1(): Long { | |
val prevFive = ArrayDeque<Long>() | |
prevFive.addAll(input.subList(0, 25)) | |
for (i in 25 until input.size) { | |
val elem = input[i] | |
if (!prevFive.any { prevFive.contains(elem - it) }) return elem | |
prevFive.removeFirst() | |
prevFive.addLast(elem) | |
} | |
return -1 | |
} | |
fun part2(): Long { | |
val target = part1() | |
for (i in input.indices) { | |
for (j in i + 1 until input.size) { | |
val range = input.subList(i, j+1) | |
if (range.sum() == target) return range.minOrNull()!! + range.maxOrNull()!! | |
} | |
} | |
return -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment