Skip to content

Instantly share code, notes, and snippets.

@calebwilson706
Created December 15, 2020 17:00
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 calebwilson706/b4b94a93084b58c82658fcc3cc8da2e4 to your computer and use it in GitHub Desktop.
Save calebwilson706/b4b94a93084b58c82658fcc3cc8da2e4 to your computer and use it in GitHub Desktop.
AOC day 15 Kotlin
fun main(){
val time = measureTimeMillis { println(findNumber(at = 29999999)) }
println(time/1000)
}
fun findNumber(at : Int) : Int {
val my_puzzle = mutableListOf<Int>(0,12,6,13,20,1,17)
var lastRegistered = mutableMapOf<Int, Int>()
for (x in 0..my_puzzle.lastIndex) lastRegistered[my_puzzle[x]] = x
for (x in (my_puzzle.size - 1)..at) {
if (lastRegistered[my_puzzle[x]] == null){
lastRegistered[my_puzzle[x]] = my_puzzle.lastIndex
my_puzzle.add(0)
} else {
my_puzzle.add(my_puzzle.lastIndex - lastRegistered[my_puzzle[x]]!!)
lastRegistered[my_puzzle[x]] = my_puzzle.lastIndex - 1
}
}
return (my_puzzle[at])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment