Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created June 18, 2021 12:21
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 sungkmi/d291c0d49454fdc5798b44eae0f67033 to your computer and use it in GitHub Desktop.
Save sungkmi/d291c0d49454fdc5798b44eae0f67033 to your computer and use it in GitHub Desktop.
package sungkmi.aoc2020.day25
def loopSize(subjectNumber: Int, pubKey: Int): Int =
@annotation.tailrec
def loop(v: Int, i: Int): Int =
if v == pubKey then i else
val v1 = ((v.toLong * subjectNumber) % 20201227).toInt
loop(v1, i + 1)
loop(1, 0)
def encryptionKey(sub1: Int, sub2: Int): Int =
val loopsize1 = loopSize(7, sub1)
println(s"===> Loopsize #1: $loopsize1")
(0 until loopsize1).foldLeft(1){ (v, i) =>
((v.toLong * sub2) % 20201227).toInt
}
def solve1(s: String): Int =
val Array(sub1, sub2) = s.split("\n").map(_.toInt)
encryptionKey(sub1, sub2)
def solve2(s: String): Int = ???
@main def part1: Unit =
val ans = solve1(input)
println(ans)
@main def part2: Unit =
val ans = solve2(input)
println(ans)
lazy val input: String = """11349501
5107328
"""
package sungkmi.aoc2020.day25
class Day25Test extends munit.FunSuite {
test("day25 loopSize") {
assertEquals(loopSize(7, 5764801), 8)
assertEquals(loopSize(7, 17807724), 11)
}
test("day25 encryptionKey") {
assertEquals(encryptionKey(5764801, 17807724), 14897079)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment