Skip to content

Commit

Permalink
Solve 2024 day 18 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 18, 2024
1 parent e476932 commit 5a0f957
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/scala/eu/sim642/adventofcode2024/Day18.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ object Day18 {
BFS.search(graphSearch).target.get._2
}

// TODO: deduplicate
def exitSteps2(bytes: Seq[Pos], max: Pos = Pos(70, 70), after: Int = 1024): Boolean = {
val fallenBytes = bytes.take(after).toSet

val graphSearch = new GraphSearch[Pos] with UnitNeighbors[Pos] with TargetNode[Pos] {
override val startNode: Pos = Pos.zero

override def unitNeighbors(pos: Pos): IterableOnce[Pos] = {
for {
offset <- Pos.axisOffsets
newPos = pos + offset
if Pos.zero <= newPos && newPos <= max
if !fallenBytes(newPos)
} yield newPos
}

override val targetNode: Pos = max
}

BFS.search(graphSearch).target.isDefined
}

def findBlockingByte(bytes: Seq[Pos], max: Pos = Pos(70, 70)): String = {
// TODO: optimize (~5.7s)
val after = (0 to bytes.size).find(after => !exitSteps2(bytes, max, after)).get
val afterPos = bytes(after - 1)
s"${afterPos.x},${afterPos.y}"
}

def parseByte(s: String): Pos = s match {
case s"$x,$y" => Pos(x.toInt, y.toInt)
}
Expand All @@ -36,5 +65,6 @@ object Day18 {

def main(args: Array[String]): Unit = {
println(exitSteps(parseBytes(input)))
println(findBlockingByte(parseBytes(input)))
}
}
8 changes: 8 additions & 0 deletions src/test/scala/eu/sim642/adventofcode2024/Day18Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ class Day18Test extends AnyFunSuite {
test("Part 1 input answer") {
assert(exitSteps(parseBytes(input)) == 304)
}

test("Part 2 examples") {
assert(findBlockingByte(parseBytes(exampleInput), Pos(6, 6)) == "6,1")
}

ignore("Part 2 input answer") { // TODO: optimize (~5.7s)
assert(findBlockingByte(parseBytes(input)) == "50,28")
}
}

0 comments on commit 5a0f957

Please sign in to comment.