Skip to content

Commit f675566

Browse files
committed
Day 6: part 1 WIP
1 parent 9218e5d commit f675566

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

src/main/scala/Day06.scala

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import Day06.Cell.*
2+
import Day06.Direction.*
3+
import cats.data.Store
4+
import cats.derived.*
5+
import cats.syntax.all.*
6+
7+
object Day06:
8+
9+
case class Pos(row: Int, col: Int):
10+
11+
def left: Pos = Pos(row, col - 1)
12+
def up: Pos = Pos(row - 1, col)
13+
def right: Pos = Pos(row, col + 1)
14+
def down: Pos = Pos(row + 1, col)
15+
16+
def next(dir: Direction): Pos = dir match {
17+
case Left => left
18+
case Up => up
19+
case Right => right
20+
case Down => down
21+
}
22+
23+
enum Direction:
24+
case Left, Up, Right, Down
25+
26+
def turnedRight: Direction = this match {
27+
case Left => Up
28+
case Up => Right
29+
case Right => Down
30+
case Down => Left
31+
}
32+
33+
object Direction:
34+
def parse(c: Char): Option[Direction] = c match {
35+
case '<' => Left.some
36+
case '^' => Up.some
37+
case '>' => Right.some
38+
case 'v' => Down.some
39+
case _ => None
40+
}
41+
42+
enum Tile:
43+
case Empty, Obstruction
44+
45+
enum Cell:
46+
case TileCell(tile: Tile)
47+
case GuardCell(direction: Direction)
48+
49+
type LabWithNoGuardStore = Store[Pos, Option[Tile]]
50+
51+
type LabStore = Store[Pos, Option[(Tile, Guard)]]
52+
53+
def extractGuard(store: LabStore): Option[Guard] = store.extract.map(_._2)
54+
55+
case class Guard(pos: Pos, direction: Direction):
56+
57+
def updated(store: LabStore): Option[Guard] =
58+
val nextPos = pos.next(direction)
59+
store
60+
.peek(nextPos)
61+
.map(_._1 match {
62+
case Tile.Empty => Guard(nextPos, direction)
63+
case Tile.Obstruction => Guard(pos, direction.turnedRight)
64+
})
65+
66+
case class Lab(cells: List[List[Cell]], guard: Guard):
67+
68+
val toVectors: Vector[Vector[Cell]] = cells.map(_.toVector).toVector
69+
70+
val outOfBoundsPos: Pos = Pos(row = -1, col = -1)
71+
72+
val toStoreWithNoGuard: LabWithNoGuardStore =
73+
Store(
74+
p =>
75+
toVectors
76+
.get(p.row)
77+
.flatMap(_.get(p.col).map {
78+
case TileCell(tile) => tile
79+
case GuardCell(_) => Tile.Empty
80+
}),
81+
s = guard.pos
82+
)
83+
84+
def withGuard(store: LabWithNoGuardStore): LabStore = store.map(_.map((_, guard)))
85+
86+
def step(store: LabStore): LabStore =
87+
extractGuard(store).fold(ifEmpty = store) { oldGuard =>
88+
oldGuard
89+
.updated(store)
90+
.map(newGuard => store.seek(newGuard.pos).map(_.map((tile, _) => (tile, newGuard))))
91+
.getOrElse(store.seek(outOfBoundsPos)) // TODO: improve! 🔥🔥🔥
92+
}
93+
94+
def allDistinctGuardPositions(store: LabWithNoGuardStore): List[Pos] =
95+
List
96+
.unfold(init = withGuard(store))(currentStore =>
97+
extractGuard(currentStore).map(guard => (guard.pos, step(currentStore)))
98+
)
99+
.distinct
100+
101+
object Lab:
102+
def parse(rows: List[String]): Option[Lab] = ??? // rows.map(_.toList)

src/test/scala/Day06Suite.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Day06.*
2+
import Day06Suite.*
3+
import munit.ScalaCheckSuite
4+
import org.scalacheck.Gen
5+
import org.scalacheck.Prop.*
6+
7+
class Day06Suite extends ScalaCheckSuite:
8+
9+
test("42 == 42"):
10+
assertEquals(42, 42)
11+
12+
object Day06Suite:
13+
14+
val bigInput: List[String] = getLinesFromFile("src/test/scala/day06_input.txt")

src/test/scala/day06_input.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
..........#....#..........#......#.......................#...#..........................................#.........................
2+
...................#.........#......#.....#.......................#......................#...#....................................
3+
...........#..#....................................................#..........................#................#......#...........
4+
.....#.#...........................................................................#..........#................#..................
5+
...#..........................................#...........................#....................#..#.........#.....................
6+
...........#.#...#..........#...............#..................#.............#...................................#............#...
7+
......................................................................................................................#..#......#.
8+
....................#..........#.........#.....#.......#....#........................................#..#..........#...........#..
9+
....................#......#...................................................#............................................#.....
10+
..................................................#.........................#...........................#...............#.........
11+
#....#....#..................#.........#..#...................#.........#....#.....#.#........................................#...
12+
...........................................#..............................................................#.......................
13+
#...............................................................................................#...............#...........##....
14+
...#......#.............#...............#.....#........................................#.#........#...........#...#...............
15+
#........#......#............................#.......................#............#...........................#...................
16+
............#................................................#....................................................#...............
17+
.......#.................#......................#.....................#..........#................................................
18+
.......................................................................................#.....#......#.............................
19+
...........................#..#....#.......................#.............................................#...#......#.............
20+
........#........##...#.....................................................#...........................................#.#.......
21+
...............................#.........#......................#...........#..........#.....#...........#.#.................#....
22+
..#.#.............#................................................................................#..................#...........
23+
.....................#.................#............................................#...................#...................#.#.#.
24+
........#...........................................................................................#............##...............
25+
........................................................#........................................................##...............
26+
..#........#.................#...............#......#..........................................#................#.................
27+
..................#......#.............................#.............................#.....................#...#........#........#
28+
....................................#.#.........#..#...............#...............#..............................................
29+
...##..................................................................#....#..............#.#...............................#....
30+
...........#....#...#........#..............................................#.......#.............................................
31+
.................................#.............................#...................................................#..............
32+
..........#.............................#.....#...............#..........................................##...............#.......
33+
...........................................#......##...............#..............#.............................#...#............#
34+
.#.#.....................#........#........................................................#..........#.........#.................
35+
.............#.......................................##..........#.#..........................................................#...
36+
.............#....................................................................................................................
37+
..................##.....#......#........................#......................#........#........................................
38+
.........................................................................................#.....#............#....................#
39+
.#...........#....................................#.#.....................................................#......#...#............
40+
#.#...#....................................................................................#............................##.......#
41+
...#...............#................#...#...#.......................#................................#.#....#.....................
42+
................................#........................................#...................#....................................
43+
...#..#.........#......#......................#..#.....................#..........................................................
44+
.................................................................#.......#........................................#...............
45+
..................................##.......................##........................................#............................
46+
....#....#.............#........................................................#....#.......#....................#..............#
47+
.........##.............................................................#..#.#.....#...........##.................................
48+
....#.........................................#......#.......................#....#..............................#...........#....
49+
.##.....#...........................#........#...................................................#....#...........................
50+
...........................................................#.....#.#...................#......................#......#............
51+
....#............#......................................................#.......#............................................#....
52+
#.......................#..........#.#............................#........#..................................................#...
53+
#....................#.....#......................................................#....................................#.....#....
54+
...#.....#.....#.................#...........#...............................................#....................................
55+
...........................#..........................#............#............#...........................................#.....
56+
.......#.....#..........#........#...................................................................................#........#...
57+
#.....................................#......................................#.............................#......#...............
58+
#.....#....................#.........................................................#................#...........................
59+
.........................................................#....#............#..##.....................#............................
60+
...#..............#.................................#.......#...................................................................#.
61+
............................#........#...........#.......................#.......................#..........#..........#..........
62+
....#..............#.............................................................................#.........#.....#...........#....
63+
..........#................#.........................................#.#..#......................................#...#............
64+
............#...............................................................................................#.#.......#......#..#.
65+
.#................................#...............#.........................#............................................#........
66+
....................................................#.......................#........#........................#.................#.
67+
..###...............#...............#..........................................#.............#...............#..#.#...............
68+
..#...#.......................................#...................................................................#...#...........
69+
..........#..........#..#......#.............................................#......#..................#...................#.....#
70+
.......#.....#.......#.......#.....................................................................................#..........#.#.
71+
#........................#..#......................................................#......#...........#................##.........
72+
....#.....................#...........................................................................#...........................
73+
.......................#..........................#.............................................................................#.
74+
.........#..............#...................#................#.........................#..........#...............................
75+
.........#...........................................................#................................................#......#....
76+
.................#.....#................#.............................................................#...................#......#
77+
....................#.....................................................#...................................................#...
78+
............##.................#...................#................#.......................................................#.....
79+
.........................#............#.#..........................................................................#........#.....
80+
............#.........#........................#..............................................##....#.......................#.....
81+
......#...................................................#..##................^......#...#.......................................
82+
............#.....................................................#..............#.##.....#...............................##......
83+
....................#...................#......#......#......................................................#.....#...#..........
84+
..........................................................................#....#..........#........#..............................
85+
.#.................................................#..............................................................................
86+
.......................#.............................................................#.....#..................#.................#.
87+
...#...............#.#................................#..............................#.....#..#.....#..........#...............#..
88+
...........................................#.............#................................................................#.......
89+
#..............#................................................................................................................#.
90+
..................................#.#................##..............#......#..................#......................#...........
91+
..........#...................................#..................................................#................#............#..
92+
....#.....................#......................#.....#.........#........#................#....#.................................
93+
................................................#......................#.....#....................................................
94+
......#.....#.........................#..........................................................................................#
95+
............................................#..............#.................................#......................#.............
96+
...#......#................................#...............#................................................#.....#...............
97+
#...............#.........................#.#..........................................................#......................#...
98+
..#........#......................#.........#............#...###.........#....................................................#...
99+
............#.....................#........................................#..............#.........#........#....................
100+
...#....#...........................................#.................#...............#.........................#.................
101+
......#........................#..................................................................................................
102+
...#..........................................#....................................................#...........#....##.#.........#
103+
....................#..........................#....#..#...........................................................#..............
104+
.....#...........#.........................#...............#..........................................#..#.#.....................#
105+
................#.......#......#.............................................................#...#................................
106+
...........#....#........##.....................#.....#.....#.....#.........................................................#.....
107+
.#.#.........................#...#......#................................#.......##......#..........#...#..........##.............
108+
...................#...................#......#............................................#...........#.......................#..
109+
..............#.#.................................#.....................................................................#.........
110+
......#............#...................................##....................................#...................#.#..............
111+
......................#.............................#....................................................#...............#........
112+
..........................#.......................................................................#.......##...#..................
113+
...................................................#..............#...#.............#.................#..........#.#....#.........
114+
...#......#....................#..................................................................#...............#......#........
115+
....#......#.#.........#..#..#......#.....................................................#.................#.....#...............
116+
......#..........#............#..............#.......#..................................#.........................................
117+
.##.#.........................#..#.............#..........#.........................................................#.............
118+
.....#..#......................##...........#........................#..#.............................................#...........
119+
.....#...............................#................#...........................................................................
120+
.##....#...................#............#....................................#..............##.............#................##....
121+
....................#......##..............................#...#.................#...............##...#............#..............
122+
.................#...........................#.....................................#.#........#...........#.........#....##.......
123+
......#.............................##....................#......................................................................#
124+
...........#........##...#......#...........#................#.....................................................#..............
125+
......#.#.................................#...........................................................#...#......#........#.#....#
126+
...........#......#....#...............#.......#................#.....................#...........................................
127+
....#......#............................##.#...............##.#..........#.........#........#.................................#...
128+
.#............................#................................#.#..............................................#.....#........#..
129+
.#.#.......#.......#.....................................................................#..............#.................##......
130+
.....#.......................................#.......#........#..............#.........#................#...#...#.................

0 commit comments

Comments
 (0)