Some effort to optimize day17

This commit is contained in:
Natsu Kagami 2023-12-18 11:19:57 +01:00
parent 062758bb00
commit 10834e696b
Signed by: nki
GPG key ID: 55A032EB38B49ADB

View file

@ -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 left(using Seq[Int]) = next(Dir.fromOrdinal((dir.ordinal + 3) % 4))
inline def right(using Seq[Int]) = next(Dir.fromOrdinal((dir.ordinal + 1) % 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: object Walk:
// private val visited = mutable.Map.empty[State, Int] // private val visited = mutable.Map.empty[State, Int]
@ -62,8 +62,27 @@ object Walk:
inline def +=(inline p: (State, Int)) = p match inline def +=(inline p: (State, Int)) = p match
case (s, v) => arr(idx(s.x, s.y, s.dir)) = v case (s, v) => arr(idx(s.x, s.y, s.dir)) = v
private val q = // private val q =
mutable.PriorityQueue.empty[(Int, State)](using scala.math.Ordering.by((v, _) => -v)) // 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] = def go()(using Seq[Int]): Option[Int] =
Seq(Up, Down, Left, Right).foreach: dir => Seq(Up, Down, Left, Right).foreach: dir =>