-
-
Save waynejo/35d53607bc40972ff79909bf26ed5d52 to your computer and use it in GitHub Desktop.
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
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