Skip to content

Commit

Permalink
feat: pos and dir methods
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Dec 8, 2024
1 parent e0274be commit 62968c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
7 changes: 0 additions & 7 deletions 2024/day06.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import prelude.*
import scala.annotation.tailrec
import scala.collection.parallel.CollectionConverters.*

extension (d: Dir)
def turnRight = d match
case Dir.Up => Dir.Right
case Dir.Down => Dir.Left
case Dir.Left => Dir.Up
case Dir.Right => Dir.Down

case class Context(size: Size, pos: Pos)(walls: Set[Pos]):
def at(pos: Pos) = Option.when(size(pos))(walls(pos))
def walk =
Expand Down
23 changes: 20 additions & 3 deletions prelude/math.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ extension (n: Int)
inline infix def moddiv(d: Int) = (n % d, n / d)

final case class Pos(x: Int, y: Int):
def +(p: Pos) = Pos(x + p.x, y + p.y)
def -(p: Pos) = Pos(x - p.x, y - p.y)
def *(n: Int) = Pos(x * n, y * n)
inline def +(p: Pos) = Pos(x + p.x, y + p.y)
inline def -(p: Pos) = Pos(x - p.x, y - p.y)
inline def *(n: Int) = Pos(x * n, y * n)
inline infix def manhattan(p: Pos): Int = (x - p.x).abs + (y - p.y).abs
inline def manhattan: Int = manhattan(Pos.zero)

object Pos:
val zero = Pos(0, 0)

final case class Size(width: Int, height: Int):
inline def contains(p: Pos) =
Expand All @@ -28,3 +33,15 @@ enum Dir:
case Down => Pos(0, 1)
case Left => Pos(-1, 0)
case Right => Pos(1, 0)

def turnRight = this match
case Dir.Up => Dir.Right
case Dir.Down => Dir.Left
case Dir.Left => Dir.Up
case Dir.Right => Dir.Down

def turnLeft = this match
case Dir.Up => Dir.Left
case Dir.Down => Dir.Right
case Dir.Left => Dir.Down
case Dir.Right => Dir.Up

0 comments on commit 62968c8

Please sign in to comment.