Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active January 14, 2022 13:09
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 waynejo/35d53607bc40972ff79909bf26ed5d52 to your computer and use it in GitHub Desktop.
Save waynejo/35d53607bc40972ff79909bf26ed5d52 to your computer and use it in GitHub Desktop.
import java.io.FileInputStream
import scala.annotation.tailrec
import scala.io.StdIn
def solve6(inputs: Vector[Int], remainDays: Int): BigInt =
val maxLanternfishDay = 9
val lanternfish = inputs.foldLeft(Vector.fill[BigInt](maxLanternfishDay)(0)) { (acc ,x) =>
acc.updated(x, acc(x) + 1)
}
@tailrec
def simulateDay(lanternfish: Vector[BigInt], remainDays: Int): BigInt =
remainDays match {
case 0 =>
lanternfish.sum
case _ =>
val nextLanternfish = lanternfish.tail :+ lanternfish.head
simulateDay(nextLanternfish.updated(6, nextLanternfish(6) + lanternfish.head), remainDays - 1)
}
end simulateDay
simulateDay(lanternfish, remainDays)
end solve6
def solve6_1(inputs: Vector[Int]): BigInt =
solve6(inputs, 80)
def solve6_2(inputs: Vector[Int]): BigInt =
solve6(inputs, 256)
@main def solve6(): Unit =
val in = new FileInputStream("example6-2.in")
System.setIn(in)
val inputs = StdIn.readLine().split(",").map(_.toInt).toVector
println(solve6_1(inputs)) // 5934 387413
println(solve6_2(inputs)) // 26984457539 1738377086345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment