Skip to content

Commit

Permalink
simplify surrounding (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha authored Dec 3, 2023
1 parent 7b859bf commit deff576
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions docs/2023/puzzles/day03.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ To compute the surrounding elements of some `Symbol` or `Number`, define `surrou
```scala
def surrounds[E <: Element](y: Int, from: Element, rows: IArray[IArray[E]]): List[E] =
val (x0, y0, x1, y1) = (from.x - 1, y - 1, from.x + from.length, y + 1)
val width = x0 to x1
def overlaps(e: Element) =
val eWidth = e.x to (e.x + e.length - 1)
width.min <= eWidth.max && width.max >= eWidth.min
def overlaps(e: Element) = x0 <= (e.x + e.length - 1) && x1 >= e.x
def findUp =
if y0 < 0 then Nil
else rows(y0).filter(overlaps).toList
Expand Down Expand Up @@ -122,10 +119,7 @@ def parse(input: String): Grid =

def surrounds[E <: Element](y: Int, from: Element, rows: IArray[IArray[E]]): List[E] =
val (x0, y0, x1, y1) = (from.x - 1, y - 1, from.x + from.length, y + 1)
val width = x0 to x1
def overlaps(e: Element) =
val eWidth = e.x to (e.x + e.length - 1)
width.min <= eWidth.max && width.max >= eWidth.min
def overlaps(e: Element) = x0 <= (e.x + e.length - 1) && x1 >= e.x
def findUp =
if y0 < 0 then Nil
else rows(y0).filter(overlaps).toList
Expand Down

0 comments on commit deff576

Please sign in to comment.