Cleanup
This commit is contained in:
@@ -1,51 +1,40 @@
|
|||||||
package dev.ctsk.aoc.days
|
package dev.ctsk.aoc.days
|
||||||
|
|
||||||
import dev.ctsk.aoc._
|
import dev.ctsk.aoc._
|
||||||
import scala.collection.mutable
|
import scala.collection.mutable.{Map => MuMap}
|
||||||
|
|
||||||
object Day11 extends Solver(11):
|
object Day11 extends Solver(11):
|
||||||
// upper and lower halves of number
|
private def halves(n: Long): Option[(Long, Long)] =
|
||||||
def halves(n: Long): Option[(Long, Long)] =
|
|
||||||
val numDigits = (Math.log10(n) + 1).toLong
|
val numDigits = (Math.log10(n) + 1).toLong
|
||||||
numDigits % 2 match
|
if numDigits % 2 == 1 then None
|
||||||
case 1 => None
|
else
|
||||||
case 0 => {
|
val mod = Math.pow(10, numDigits / 2).toLong
|
||||||
Some(
|
Some(n % mod, n / mod)
|
||||||
(
|
|
||||||
n % Math.pow(10, numDigits / 2).toLong,
|
|
||||||
n / Math.pow(10, numDigits / 2).toLong
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
def counter_compute(stones: Array[Long], depth: Long): Long =
|
private def count(initial: Seq[Long], depth: Int): Long =
|
||||||
var acc: mutable.Map[Long, Long] =
|
def step(stones: MuMap[Long, Long]): MuMap[Long, Long] =
|
||||||
mutable.Map.from(stones.map((_, 1L)))
|
val next = MuMap.empty[Long, Long].withDefaultValue(0L)
|
||||||
|
for ((stone, count) <- stones) {
|
||||||
for (_ <- 0L until depth) {
|
|
||||||
val next = mutable.Map.empty[Long, Long].withDefaultValue(0L)
|
|
||||||
for ((stone, count) <- acc) {
|
|
||||||
if stone == 0 then next(1) += count
|
if stone == 0 then next(1) += count
|
||||||
else
|
else
|
||||||
halves(stone.toLong) match
|
halves(stone) match
|
||||||
case Some((a, b)) =>
|
case Some((a, b)) =>
|
||||||
next(a) += count
|
next(a) += count
|
||||||
next(b) += count
|
next(b) += count
|
||||||
case None =>
|
case None =>
|
||||||
next(stone * 2024) += count
|
next(stone * 2024) += count
|
||||||
}
|
}
|
||||||
acc = next
|
next
|
||||||
}
|
|
||||||
|
|
||||||
acc.values.sum
|
val initMap = MuMap.from(initial.map((_, 1L)))
|
||||||
|
Seq.iterate(initMap, depth + 1)(step).last.values.sum
|
||||||
|
|
||||||
def run(input: os.ReadablePath): (Timings, Solution) =
|
def run(input: os.ReadablePath): (Timings, Solution) =
|
||||||
val in = longs(os.read.lines(input).head).toArray
|
val (pre_time, in) = timed { longs(os.read.lines(input).head) }
|
||||||
|
val (p1_time, p1_solution) = timed { count(in, 25) }
|
||||||
val p1 = timed { counter_compute(in, 25) }
|
val (p2_time, p2_solution) = timed { count(in, 75) }
|
||||||
val p2 = timed { counter_compute(in, 75) }
|
|
||||||
|
|
||||||
(
|
(
|
||||||
Timings(0, p1._1, p2._1),
|
Timings(pre_time, p1_time, p2_time),
|
||||||
Solution(Long.box(p1._2), Long.box(p2._2))
|
Solution(Long.box(p1_solution), Long.box(p2_solution))
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user