This commit is contained in:
Alexander Gehrke 2021-12-06 18:35:04 +01:00
parent 84c42954ce
commit f847835e50
3 changed files with 17 additions and 1 deletions

View file

@ -0,0 +1 @@
3,4,3,1,2

1
input/2021/day6.txt Normal file
View file

@ -0,0 +1 @@
4,1,4,1,3,3,1,4,3,3,2,1,1,3,5,1,3,5,2,5,1,5,5,1,3,2,5,3,1,3,4,2,3,2,3,3,2,1,5,4,1,1,1,2,1,4,4,4,2,1,2,1,5,1,5,1,2,1,4,4,5,3,3,4,1,4,4,2,1,4,4,3,5,2,5,4,1,5,1,1,1,4,5,3,4,3,4,2,2,2,2,4,5,3,5,2,4,2,3,4,1,4,4,1,4,5,3,4,2,2,2,4,3,3,3,3,4,2,1,2,5,5,3,2,3,5,5,5,4,4,5,5,4,3,4,1,5,1,3,4,4,1,3,1,3,1,1,2,4,5,3,1,2,4,3,3,5,4,4,5,4,1,3,1,1,4,4,4,4,3,4,3,1,4,5,1,2,4,3,5,1,1,2,1,1,5,4,2,1,5,4,5,2,4,4,1,5,2,2,5,3,3,2,3,1,5,5,5,4,3,1,1,5,1,4,5,2,1,3,1,2,4,4,1,1,2,5,3,1,5,2,4,5,1,2,3,1,2,2,1,2,2,1,4,1,3,4,2,1,1,5,4,1,5,4,4,3,1,3,3,1,1,3,3,4,2,3,4,2,3,1,4,1,5,3,1,1,5,3,2,3,5,1,3,1,1,3,5,1,5,1,1,3,1,1,1,1,3,3,1

View file

@ -3,4 +3,18 @@ package de.qwertyuiop.aoc.`2021`
import de.qwertyuiop.aoc.lib.* import de.qwertyuiop.aoc.lib.*
import cats.*, cats.implicits.given import cats.*, cats.implicits.given
def day6(using InputSource): Unit = ??? def day6(using InputSource): Unit =
// list of staring fish ages
val agesRaw = input(_.splitNN(",").map(_.toInt).toVector).head
// Vector where agesStart(i) == number of fish with age i
val agesStart = (Vector.iterate(0, 9)(_ + 1) ++ agesRaw).groupBy(identity).toVector.sorted.map(_._2.size.toLong - 1L)
def step(ages: Vector[Long]): Vector[Long] =
val next = (ages.tail :+ (ages.head)) // reduce timer for all fish, timer 0 => new fish with age 8
next.updated(6, next(6) + ages.head) // timer 0 => fish return to timer 6
val after80 = (0 until 80).foldLeft(agesStart)((ages, _) => step(ages))
val after256 = (80 until 256).foldLeft(after80)((ages, _) => step(ages))
println(s"""Fish after 80 generations: ${after80.sum})
|Fish after 256 generations: ${after256.sum}""".stripMargin