Skip to content

Commit d0696f5

Browse files
committed
Day 4: part 1 WIP
1 parent f78b693 commit d0696f5

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

src/main/scala/Day04.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cats.data.Store
2+
import cats.syntax.all.*
3+
4+
object Day04:
5+
6+
// val s: Store[Int, Option[Char]] = Store(f = _ => Some('a'), s = 0)
7+
// val x: Option[Char] = s.peeks(identity)
8+
// val y: List[Option[Char]] = s.experiment[List](n => List.range(start = n, end = n + 3))
9+
10+
case class Pos(row: Int, col: Int)
11+
12+
object Pos:
13+
val zero = Pos(row = 0, col = 0)
14+
15+
case class Grid(rows: List[List[Char]]):
16+
17+
private val toVectors: Vector[Vector[Char]] = rows.map(_.toVector).toVector
18+
19+
def toStore: Store[Pos, Option[Char]] = Store(
20+
p => toVectors.get(p.row).flatMap(_.get(p.col)),
21+
s = Pos.zero
22+
)
23+
24+
def allPositions: List[Pos] =
25+
rows.zipWithIndex.flatMap((row, rowIndex) => row.zipWithIndex.map((_, colIndex) => Pos(rowIndex, colIndex)))
26+
27+
object Grid:
28+
def parse(rows: List[String]): Option[Grid] = Grid(rows = rows.map(_.toList)).some

src/test/scala/Day04Suite.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Day04.*
2+
import Day04Suite.*
3+
import cats.syntax.all.*
4+
import munit.ScalaCheckSuite
5+
import org.scalacheck.Gen
6+
import org.scalacheck.Prop.*
7+
8+
class Day04Suite extends ScalaCheckSuite:
9+
10+
test("small input parsed correctly"):
11+
assertEquals(
12+
Grid.parse(rows = List("abc", "def")),
13+
Grid(rows = List(List('a', 'b', 'c'), List('d', 'e', 'f'))).some
14+
)
15+
16+
test("big input parsed to something"):
17+
assert(Grid.parse(rows = bigInput).isDefined)
18+
19+
test("Grid.allPositions example"):
20+
assertEquals(
21+
Grid(rows = List(List('a', 'b', 'c'), List('d', 'e', 'f'))).allPositions,
22+
List(
23+
Pos(row = 0, col = 0),
24+
Pos(row = 0, col = 1),
25+
Pos(row = 0, col = 2),
26+
Pos(row = 1, col = 0),
27+
Pos(row = 1, col = 1),
28+
Pos(row = 1, col = 2)
29+
)
30+
)
31+
32+
property("Grid |allPositions| = rows * columns"):
33+
forAll(nonEmptyGridGen)(neg => assertEquals(neg.allPositions.length, neg.rows.length * neg.rows.head.length))
34+
35+
object Day04Suite:
36+
37+
val bigInput: List[String] = getLinesFromFile("src/test/scala/day04_input.txt")
38+
39+
val nonEmptyGridGen: Gen[Grid] = Gen.zip(Gen.choose(1, 10), Gen.choose(1, 10)).flatMap { (r, c) =>
40+
Gen.listOfN(r, Gen.listOfN(c, Gen.alphaUpperChar)).map(Grid.apply)
41+
}

0 commit comments

Comments
 (0)