Skip to content

Instantly share code, notes, and snippets.

@saidaspen
Created December 9, 2020 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saidaspen/1c505d416c0be3b6df4177ca4210ca71 to your computer and use it in GitHub Desktop.
Save saidaspen/1c505d416c0be3b6df4177ca4210ca71 to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day 9
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