Skip to content

Commit

Permalink
🎨 distinguish Seq[Seq[T]] from Seq[String]
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 4, 2024
1 parent 1d16da0 commit ab196f4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/scala/org/lemon/advent/lib/2d/Area.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ object Area:
given Conversion[Area, Iterator[Coord]] =
(area: Area) => for y <- area.yRange.iterator; x <- area.xRange.iterator yield (x, y)

extension [T](seq: Seq[String])
extension [T](seq: Seq[String])(using String => WrappedString)
def apply(area: Area): Seq[Seq[Char]] = seq.slice(area.top, area.bottom + 1).map(_.slice(area.left, area.right + 1))

extension [T](seq: Seq[Seq[T]])
def apply(area: Area): Seq[Seq[T]] = seq.slice(area.top, area.bottom + 1).map(_.slice(area.left, area.right + 1))

case class Area(xRange: Range, yRange: Range):
def left = xRange.min
def right = xRange.max
Expand Down
7 changes: 6 additions & 1 deletion src/main/scala/org/lemon/advent/lib/2d/Coord.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.lemon.advent.lib.`2d`

import org.lemon.advent.lib.graph.UnitGraph

import scala.collection.immutable.WrappedString

object Coord:

given Conversion[(Int, Int), Coord] = (coord: (Int, Int)) => Coord(x = coord._1, y = coord._2)
Expand All @@ -20,9 +22,12 @@ object Coord:
def hasCoord(coord: Coord): Boolean =
seq.indices.contains(coord.row) && seq(coord.row).indices.contains(coord.col)

extension (seq: Seq[String])
extension (seq: Seq[String])(using String => WrappedString)
def apply(coord: Coord): Char = seq(coord.row)(coord.col)

def hasCoord(coord: Coord): Boolean =
seq.indices.contains(coord.row) && seq(coord.row).indices.contains(coord.col)

extension [V](grid: Map[Coord, V])
def toGridAdjacencyList: UnitGraph[Coord] =
grid.map((coord, _) => (coord, coord.adjacent.filter(grid.contains)))
Expand Down

0 comments on commit ab196f4

Please sign in to comment.