diff --git a/input/2021/day6-sample1.txt b/input/2021/day6-sample1.txt new file mode 100644 index 0000000..55129f1 --- /dev/null +++ b/input/2021/day6-sample1.txt @@ -0,0 +1 @@ +3,4,3,1,2 diff --git a/input/2021/day6.txt b/input/2021/day6.txt new file mode 100644 index 0000000..156bc49 --- /dev/null +++ b/input/2021/day6.txt @@ -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 diff --git a/src/main/scala/de.qwertyuiop.aoc/2021/day6.scala b/src/main/scala/de.qwertyuiop.aoc/2021/day6.scala index e86d217..dd0f254 100644 --- a/src/main/scala/de.qwertyuiop.aoc/2021/day6.scala +++ b/src/main/scala/de.qwertyuiop.aoc/2021/day6.scala @@ -3,4 +3,18 @@ package de.qwertyuiop.aoc.`2021` import de.qwertyuiop.aoc.lib.* 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