Day 11
This commit is contained in:
@@ -12,7 +12,8 @@ val solvers = Map[Int, Solver](
|
|||||||
7 -> Day07,
|
7 -> Day07,
|
||||||
8 -> Day08,
|
8 -> Day08,
|
||||||
9 -> Day09,
|
9 -> Day09,
|
||||||
10 -> Day10
|
10 -> Day10,
|
||||||
|
11 -> Day11
|
||||||
)
|
)
|
||||||
|
|
||||||
def runSolver(solver: Solver, input: os.Path): Unit =
|
def runSolver(solver: Solver, input: os.Path): Unit =
|
||||||
|
|||||||
51
aoc/src/dev/ctsk/aoc/days/Day11.scala
Normal file
51
aoc/src/dev/ctsk/aoc/days/Day11.scala
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package dev.ctsk.aoc.days
|
||||||
|
|
||||||
|
import dev.ctsk.aoc._
|
||||||
|
import scala.collection.mutable
|
||||||
|
|
||||||
|
object Day11 extends Solver(11):
|
||||||
|
// upper and lower halves of number
|
||||||
|
def halves(n: Long): Option[(Long, Long)] =
|
||||||
|
val numDigits = (Math.log10(n) + 1).toLong
|
||||||
|
numDigits % 2 match
|
||||||
|
case 1 => None
|
||||||
|
case 0 => {
|
||||||
|
Some(
|
||||||
|
(
|
||||||
|
n % Math.pow(10, numDigits / 2).toLong,
|
||||||
|
n / Math.pow(10, numDigits / 2).toLong
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
def counter_compute(stones: Array[Long], depth: Long): Long =
|
||||||
|
var acc: mutable.Map[Long, Long] =
|
||||||
|
mutable.Map.from(stones.map((_, 1L)))
|
||||||
|
|
||||||
|
for (_ <- 0L until depth) {
|
||||||
|
val next = mutable.Map.empty[Long, Long].withDefaultValue(0L)
|
||||||
|
for ((stone, count) <- acc) {
|
||||||
|
if stone == 0 then next(1) += count
|
||||||
|
else
|
||||||
|
halves(stone.toLong) match
|
||||||
|
case Some((a, b)) =>
|
||||||
|
next(a) += count
|
||||||
|
next(b) += count
|
||||||
|
case None =>
|
||||||
|
next(stone * 2024) += count
|
||||||
|
}
|
||||||
|
acc = next
|
||||||
|
}
|
||||||
|
|
||||||
|
acc.values.sum
|
||||||
|
|
||||||
|
def run(input: os.ReadablePath): (Timings, Solution) =
|
||||||
|
val in = longs(os.read.lines(input).head).toArray
|
||||||
|
|
||||||
|
val p1 = timed { counter_compute(in, 25) }
|
||||||
|
val p2 = timed { counter_compute(in, 75) }
|
||||||
|
|
||||||
|
(
|
||||||
|
Timings(0, p1._1, p2._1),
|
||||||
|
Solution(Long.box(p1._2), Long.box(p2._2))
|
||||||
|
)
|
||||||
2
data/11.ans
Normal file
2
data/11.ans
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
189167
|
||||||
|
225253278506288
|
||||||
1
data/11.in
Normal file
1
data/11.in
Normal file
@@ -0,0 +1 @@
|
|||||||
|
28 4 3179 96938 0 6617406 490 816207
|
||||||
Reference in New Issue
Block a user