Created
December 15, 2020 17:00
-
-
Save calebwilson706/b4b94a93084b58c82658fcc3cc8da2e4 to your computer and use it in GitHub Desktop.
AOC day 15 Kotlin
This file contains 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 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