Commit 54c3d2cc authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[day25] solution

      馃専
     /  \
    < 馃暞  >
   /   馃數 \
  < 馃敶  馃暞  >
 / 馃暞     馃煝 \
<_____  _____>
     /__\
parent 74a03ce1
3418282
8719412
package aoc2020
object day25 extends (List[Long] => String) {
def apply(input: List[Long]): String =
val loopSizes = input.map(findLoopSize(7, _))
val encryptionKey = transform(input(0), loopSizes(1))
s"Encryption Key: $encryptionKey"
val module = 20201227L
def loopStep(current: Long, subject: Long): Long = current * subject % module
@annotation.tailrec
def transform(subject: Long, loopSize: Long, current: Long = 1): Long =
if loopSize == 0 then current
else transform(subject, loopSize - 1, loopStep(current, subject))
def findLoopSize(subject: Long, pubKey: Long): Long =
def rec(current: Long, iterations: Long = 0): Long =
if current == pubKey then iterations
else rec(loopStep(current, subject), iterations + 1)
rec(1)
}
......@@ -27,6 +27,7 @@ import aoc2020.lib._
case 22 => input()(day22)
case 23 => flatInputF(_.map(_.toInt - '0'))(Vector)(day23)
case 24 => input()(day24)
case 25 => input(_.toLong)(day25)
case _ => "No such day implemented"
}
if (sample.nonEmpty) println("SAMPLE VALUES!")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment