mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 05:49:52 +01:00
Move scala codebase in a directory on its own
This commit is contained in:
42
scalaAoC/2018/day14/part1.scala
Normal file
42
scalaAoC/2018/day14/part1.scala
Normal file
@@ -0,0 +1,42 @@
|
||||
import scala.io.StdIn.readInt
|
||||
|
||||
case class Turn(recipes: Vector[Int], idx1: Int, idx2: Int, turnNb: Int) {
|
||||
def newScores(a: Int, b: Int): Vector[Int] =
|
||||
(a + b).toString.map(_.toInt - '0').toVector
|
||||
|
||||
def next: Turn = {
|
||||
val newRecipes = recipes ++ newScores(recipes(idx1), recipes(idx2))
|
||||
val newIdx1 = (idx1 + (newRecipes(idx1) + 1)) % newRecipes.size
|
||||
val newIdx2 = (idx2 + (newRecipes(idx2) + 1)) % newRecipes.size
|
||||
Turn(newRecipes, newIdx1, newIdx2, turnNb + 1)
|
||||
}
|
||||
|
||||
override def toString: String =
|
||||
recipes.zipWithIndex.map {
|
||||
case (v, k) => k match {
|
||||
case _ if k == idx1 => s"($v)"
|
||||
case _ if k == idx2 => s"[$v]"
|
||||
case _ => s" $v "
|
||||
}
|
||||
}.mkString(" ")
|
||||
|
||||
def computeScoreAt(n: Int): Option[String] =
|
||||
recipes match {
|
||||
case _ if recipes.size < n + 10 => None
|
||||
case _ => Some(recipes.slice(n, n + 10).mkString(""))
|
||||
}
|
||||
}
|
||||
|
||||
val input = readInt
|
||||
|
||||
val turn0 = Turn(Vector(3, 7), 0, 1, 0)
|
||||
|
||||
val endTurn = Iterator.iterate(turn0)(_.next)
|
||||
.filter(_.recipes.length >= input + 11)
|
||||
.next
|
||||
|
||||
// println(s"Score: ${endTurn.computeScoreAt(5)} - Expected: 0124515891")
|
||||
// println(s"Score: ${endTurn.computeScoreAt(18)} - Expected: 9251071085")
|
||||
// println(s"Score: ${endTurn.computeScoreAt(2018)} - Expected: 5941429882")
|
||||
|
||||
println(s"Score: ${endTurn.computeScoreAt(input).get}")
|
||||
Reference in New Issue
Block a user