Skip to content

Commit 88d0914

Browse files
committed
perf: use single Int
1 parent 5fe5eb5 commit 88d0914

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

2024/src/day20.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ extension (x: Int) inline def ±(y: Int) = x - y to x + y
1818
extension (x: Inclusive)
1919
inline def &(y: Inclusive) = (x.start max y.start) to (x.end min y.end)
2020

21-
opaque type Pos = (Int, Int)
21+
opaque type Pos = Int
2222

2323
object Pos:
24-
val up: Pos = (0, -1)
25-
val down: Pos = (0, 1)
26-
val left: Pos = (-1, 0)
27-
val right: Pos = (1, 0)
28-
val zero: Pos = (0, 0)
29-
def apply(x: Int, y: Int): Pos = (x, y)
24+
val up = Pos(0, -1)
25+
val down = Pos(0, 1)
26+
val left = Pos(-1, 0)
27+
val right = Pos(1, 0)
28+
val zero = Pos(0, 0)
29+
inline def apply(x: Int, y: Int): Pos = y << 16 | x
3030

3131
extension (p: Pos)
32-
inline def x = p._1
33-
inline def y = p._2
32+
inline def x = p & 0xffff
33+
inline def y = p >> 16
3434
inline def neighbors: List[Pos] =
3535
List(p + up, p + right, p + down, p + left)
36-
inline def +(q: Pos): Pos = (p.x + q.x, p.y + q.y)
36+
inline def +(q: Pos): Pos = Pos(p.x + q.x, p.y + q.y)
3737
inline infix def taxiDist(q: Pos) = (p.x - q.x).abs + (p.y - q.y).abs
3838

3939
case class Rect(x: Inclusive, y: Inclusive):

0 commit comments

Comments
 (0)