Skip to content

Commit 718566b

Browse files
committed
Day 6: part 1 cleanup
1 parent b87fc5b commit 718566b

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/main/scala/Day06.scala

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

src/test/scala/Day06Suite.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import munit.ScalaCheckSuite
88
class Day06Suite extends ScalaCheckSuite:
99

1010
test("big input parsed to something"):
11-
assert(Lab.parse(rows = bigInput).isDefined)
11+
assert(Lab.parse(bigInput).isDefined)
1212

1313
test("small input parsed correctly"):
1414
assertEquals(
15-
Lab.parse(rows = smallInput),
15+
Lab.parse(smallInput),
1616
Lab(
1717
cells = List(
1818
List(e, e, e, e, o, e, e, e, e, e),
@@ -25,11 +25,16 @@ class Day06Suite extends ScalaCheckSuite:
2525
List(e, e, e, e, e, e, e, e, o, e),
2626
List(o, e, e, e, e, e, e, e, e, e),
2727
List(e, e, e, e, e, e, o, e, e, e)
28-
),
29-
Guard(Pos(row = 6, col = 4), Direction.Up)
28+
)
3029
).some
3130
)
3231

32+
test("small input guard is at pos (6, 4) and facing up"):
33+
assertEquals(
34+
Lab.parse(smallInput).map(_.guard),
35+
Guard(Pos(row = 6, col = 4), Direction.Up).some
36+
)
37+
3338
test("guard will visit 41 distinct positions in small input lab"):
3439
assertEquals(Lab.parse(smallInput).map(_.allDistinctGuardPositionsCount), 41.some)
3540

0 commit comments

Comments
 (0)