-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03ba748
commit b932673
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.lemon.advent.year2024 | ||
|
||
import org.lemon.advent.lib.`2d`._ | ||
import org.lemon.advent.lib.graph._ | ||
|
||
private object Day18: | ||
|
||
def parse(input: String) = input.linesIterator.map(_ match | ||
case s"$x,$y" => Coord(x.toInt, y.toInt) | ||
).toSeq | ||
|
||
def adjacency(area: Area, corrupt: Set[Coord])(coord: Coord): Seq[Coord] = | ||
coord.adjacent.filterNot(corrupt).filter(area.contains) | ||
|
||
def part1(input: String, example: Boolean = false, take: Int = 1024) = | ||
val area = if example then Area(0 to 6, 0 to 6) else Area(0 to 70, 0 to 70) | ||
val corrupt = parse(input).take(take).toSet | ||
pathFind(adjacency(area, corrupt), Coord(0, 0), area.bottomRight).get.distance | ||
|
||
def part2(input: String, example: Boolean = false) = | ||
val area = if example then Area(0 to 6, 0 to 6) else Area(0 to 70, 0 to 70) | ||
val bytes = parse(input) | ||
val lastPossible = bytes.reverse.tails.find(bits => | ||
pathFind(adjacency(area, bits.toSet), Coord(0, 0), area.bottomRight).isDefined | ||
).get | ||
val impossible = bytes(lastPossible.size) | ||
s"${impossible.x},${impossible.y}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.lemon.advent.year2024 | ||
|
||
import org.lemon.advent._ | ||
import org.lemon.advent.year2024.Day18._ | ||
|
||
class Day18Test extends UnitTest: | ||
|
||
test("part 1 example") { | ||
val in = """|5,4 | ||
|4,2 | ||
|4,5 | ||
|3,0 | ||
|2,1 | ||
|6,3 | ||
|2,4 | ||
|1,5 | ||
|0,6 | ||
|3,3 | ||
|2,6 | ||
|5,1 | ||
|1,2 | ||
|5,5 | ||
|2,5 | ||
|6,5 | ||
|1,4 | ||
|0,4 | ||
|6,4 | ||
|1,1 | ||
|6,1 | ||
|1,0 | ||
|0,5 | ||
|1,6 | ||
|2,0""".stripMargin | ||
part1(in, example = true, take = 12) shouldBe 22 | ||
} | ||
|
||
test("part 1") { | ||
part1(read(file(2024)(18))) shouldBe 338 | ||
} | ||
|
||
test("part 2 example") { | ||
val in = """|5,4 | ||
|4,2 | ||
|4,5 | ||
|3,0 | ||
|2,1 | ||
|6,3 | ||
|2,4 | ||
|1,5 | ||
|0,6 | ||
|3,3 | ||
|2,6 | ||
|5,1 | ||
|1,2 | ||
|5,5 | ||
|2,5 | ||
|6,5 | ||
|1,4 | ||
|0,4 | ||
|6,4 | ||
|1,1 | ||
|6,1 | ||
|1,0 | ||
|0,5 | ||
|1,6 | ||
|2,0""".stripMargin | ||
part2(in, example = true) shouldBe "6,1" | ||
} | ||
|
||
test("part 2") { | ||
part2(read(file(2024)(18))) shouldBe "20,44" | ||
} |