diff --git a/Day17.scala b/Day17.scala index 2b1e7fb..20d71fa 100644 --- a/Day17.scala +++ b/Day17.scala @@ -50,7 +50,7 @@ case class State(x: Int, y: Int, dir: Dir): inline def left(using Seq[Int]) = next(Dir.fromOrdinal((dir.ordinal + 3) % 4)) inline def right(using Seq[Int]) = next(Dir.fromOrdinal((dir.ordinal + 1) % 4)) - def all(using Seq[Int]) = left ++ right + inline def all(using Seq[Int]) = left ++ right object Walk: // private val visited = mutable.Map.empty[State, Int] @@ -62,8 +62,27 @@ object Walk: inline def +=(inline p: (State, Int)) = p match case (s, v) => arr(idx(s.x, s.y, s.dir)) = v - private val q = - mutable.PriorityQueue.empty[(Int, State)](using scala.math.Ordering.by((v, _) => -v)) + // private val q = + // mutable.PriorityQueue.empty[(Int, State)](using scala.math.Ordering.by((v, _) => -v)) + + private object q: + private inline val sz = 128 + private val arrs = mutable.ArrayBuffer.fill(sz)(mutable.ArrayBuffer.empty[State]) + private var ptr = 0 + private var size = 0 + + def +=(p: (Int, State)): this.type = + val (d, s) = p + size += 1 + arrs(d % sz).addOne(s) + this + + inline def isEmpty = size == 0 + + inline def dequeue() = + size -= 1 + while arrs(ptr % sz).isEmpty do ptr += 1 + (ptr, arrs(ptr % sz).remove(arrs(ptr % sz).size - 1)) def go()(using Seq[Int]): Option[Int] = Seq(Up, Down, Left, Right).foreach: dir =>