Skip to content

Commit

Permalink
Remove hard coded size of the grid
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Dec 10, 2023
1 parent 96d9910 commit 40205c1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 26 deletions.
6 changes: 2 additions & 4 deletions Day03.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import fs2.Stream

object Day03 extends AOCApp(2023, 3):

def part1(input: Stream[IO, String]): IO[String] =
sovle(input, _.calculateP1())
def part1(input: Stream[IO, String]): IO[String] = sovle(input, _.calculateP1())

def part2(input: Stream[IO, String]): IO[String] =
sovle(input, _.calculateP2())
def part2(input: Stream[IO, String]): IO[String] = sovle(input, _.calculateP2())

def sovle(input: Stream[IO, String], f: Schematic => Long): IO[String] =
input
Expand Down
5 changes: 1 addition & 4 deletions Day07.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ object Day07 extends AOCApp(2023, 7):
def part2(input: Stream[IO, String]) = solve(_.solveP2())(input)

def solve(f: Solution => Long): Stream[IO, String] => IO[String] =
_.compileAsList(Parser.parseLine)
.map(Solution(_))
.map(f)
.map(_.toString)
_.compileAsList(Parser.parseLine).map(Solution(_)).map(f).map(_.toString)

enum Card:
case A, K, Q, J, T, Nine, Eight, Seven, Six, Five, Four, Three, Two
Expand Down
7 changes: 1 addition & 6 deletions Day08.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ object Day08 extends AOCApp(2023, 8):
def part2(input: Stream[IO, String]): IO[String] = solve(input, _.solveP2)

def solve(input: Stream[IO, String], f: Solution => Any): IO[String] =
input
.map(Parser.parse)
.map(f)
.map(_.toString)
.compile
.lastOrError
input.map(Parser.parse).map(f).map(_.toString).compile.lastOrError

case class Pair(left: String, right: String)
enum Direction:
Expand Down
20 changes: 8 additions & 12 deletions Day10.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ object Day10 extends AOCApp(2023, 10):
def part2(input: Stream[IO, String]) = solve(input, _.solveP2())

def solve(input: Stream[IO, String], f: Solution => Any): IO[String] =
input
.map(Parser.parse)
.map(f)
.map(_.toString)
.compile
.lastOrError
input.map(Parser.parse).map(f).map(_.toString).compile.lastOrError

enum Direction(val value: Char):
case VP extends Direction('|')
Expand All @@ -31,7 +26,7 @@ object Day10 extends AOCApp(2023, 10):

type Grid = Map[Point, Direction]

case class Solution(start: Point, map: Grid):
case class Solution(start: Point, map: Grid, size: Int):
lazy val possibilities: List[Grid] =
Direction.values.toList.map(map.updated(start, _))

Expand All @@ -41,14 +36,13 @@ object Day10 extends AOCApp(2023, 10):
def solveP2(): Int =
val (map, ls) = possibilities.collectFirstSome(x => findPath(x).map(x -> _)).get
val path = map.filter(x => ls.contains(x._1))
val size = 140
val all = for
x <- List.range(0, size)
y <- List.range(0, size)
yield Point(x, y)
all.count(isInside(path, size))
all.count(isInside(path))

def isInside(path: Grid, size: Int)(p: Point): Boolean =
def isInside(path: Grid)(p: Point): Boolean =
if path.contains(p) then false
else
val n = List
Expand Down Expand Up @@ -109,6 +103,8 @@ object Day10 extends AOCApp(2023, 10):
.map: xs =>
val start = xs.collectFirst { case (p, Left(_)) => p }.get
val map = xs.collect { case (p, Right(d)) => p -> d }.toMap
Solution(start, map)
start -> map

def parse(str: String) = solution.parseAll(str).toOption.get
def parse(str: String) =
val (start, map) = solution.parseAll(str).toOption.get
Solution(start, map, str.split("\n").toList.size)

0 comments on commit 40205c1

Please sign in to comment.