Skip to content

Commit

Permalink
Solve 2023 day 23 part 2 naively
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 23, 2023
1 parent c714473 commit 4946473
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/scala/eu/sim642/adventofcode2023/Day23.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ object Day23 {
'<' -> Pos(-1, 0),
)

def longestHike(grid: Grid[Char]): Int = {
def longestHike(grid: Grid[Char], slopes: Boolean = true): Int = {

val graphTraversal = new GraphTraversal[HikePos] with UnitNeighbors[HikePos] {
override val startNode: HikePos = HikePos(Pos(1, 0), Set(Pos(1, 0))) // TODO: don't hardcode

override def unitNeighbors(hikePos: HikePos): IterableOnce[HikePos] = {
val HikePos(pos, path) = hikePos
for {
offset <- if (grid(pos) == '.') Pos.axisOffsets else Seq(slopeOffsets(grid(pos)))
offset <- if (!slopes || grid(pos) == '.') Pos.axisOffsets else Seq(slopeOffsets(grid(pos)))
newPos = pos + offset
if grid.containsPos(newPos)
if grid(newPos) != '#'
Expand All @@ -49,6 +49,7 @@ object Day23 {
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day23.txt")).mkString.trim

def main(args: Array[String]): Unit = {
println(longestHike(parseGrid(input)))
//println(longestHike(parseGrid(input)))
println(longestHike(parseGrid(input), slopes = false))
}
}
8 changes: 8 additions & 0 deletions src/test/scala/eu/sim642/adventofcode2023/Day23Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ class Day23Test extends AnyFunSuite {
ignore("Part 1 input answer") { // TODO: optimize
assert(longestHike(parseGrid(input)) == 2414)
}

test("Part 2 examples") {
assert(longestHike(parseGrid(exampleInput), slopes = false) == 154)
}

ignore("Part 2 input answer") { // TODO: optimize
assert(longestHike(parseGrid(input), slopes = false) == 2414)
}
}

0 comments on commit 4946473

Please sign in to comment.