@@ -19,6 +19,9 @@ object Day06:
1919 case Down => down
2020 }
2121
22+ object Pos :
23+ val outOfBounds : Pos = Pos (row = - 1 , col = - 1 )
24+
2225 enum Direction :
2326 case Left , Up , Right , Down
2427
@@ -63,16 +66,14 @@ object Day06:
6366
6467 type LabStore = Store [Pos , Option [(Tile , Guard )]]
6568
66- val unguardedLabStore : LabStore = {
67- val outOfBoundsPos = Pos (row = - 1 , col = - 1 )
69+ val unguardedLabStore : LabStore =
6870 Store (
6971 {
70- case Pos (row = 0 , col = 0 ) => (Tile .Empty , Guard (outOfBoundsPos, Direction . Left ) ).some
72+ case Pos (row = 0 , col = 0 ) => (Tile .Empty , Guard .outOfBounds ).some
7173 case _ => None
7274 },
73- s = outOfBoundsPos
75+ s = Pos .outOfBounds
7476 )
75- }
7677
7778 extension (store : LabStore )
7879
@@ -96,10 +97,19 @@ object Day06:
9697 case Tile .Obstruction => Guard (pos, direction.turnedRight)
9798 })
9899
99- case class Lab (cells : List [List [Cell ]], guard : Guard ):
100+ object Guard :
101+ val outOfBounds : Guard = Guard (Pos .outOfBounds, Direction .Left )
102+
103+ case class Lab (cells : List [List [Cell ]]):
100104
101105 val toVectors : Vector [Vector [Cell ]] = cells.map(_.toVector).toVector
102106
107+ val guard : Guard =
108+ cells.zipWithIndex
109+ .map((row, rowIndex) => row.zipWithIndex.map((cell, colIndex) => (Pos (rowIndex, colIndex), cell)))
110+ .collectFirstSome(_.collectFirst { case (pos, GuardCell (direction)) => Guard (pos, direction) })
111+ .getOrElse(Guard .outOfBounds)
112+
103113 val toStore : LabStore =
104114 Store (
105115 p => toVectors.get(p.row).flatMap(_.get(p.col).map(_.toTile)).map((_, guard)),
@@ -112,13 +122,4 @@ object Day06:
112122 .size
113123
114124 object Lab :
115-
116- def parse (rows : List [String ]): Option [Lab ] = for {
117- cells <- rows.traverse(_.toList.traverse(Cell .parse))
118- guard <- findGuard(cells)
119- } yield Lab (cells, guard)
120-
121- def findGuard (cells : List [List [Cell ]]): Option [Guard ] =
122- cells.zipWithIndex
123- .map((row, rowIndex) => row.zipWithIndex.map((cell, colIndex) => (Pos (rowIndex, colIndex), cell)))
124- .collectFirstSome(_.collectFirst { case (pos, GuardCell (direction)) => Guard (pos, direction) })
125+ def parse (rows : List [String ]): Option [Lab ] = rows.traverse(_.toList.traverse(Cell .parse)).map(Lab .apply)
0 commit comments