-
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
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
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,28 @@ | ||
package `2024`.day10 | ||
|
||
import prelude.* | ||
|
||
case class Context(grid: Grid[Int]): | ||
def next(x: Pos): Vector[Pos] = | ||
if grid(x) == 9 then Vector(x) | ||
else | ||
x.neighbours | ||
.withFilter(y => grid.size(y) && grid(y) == grid(x) + 1) | ||
.flatMap(next) | ||
|
||
lazy val trails = grid.where(_ == 0).map(next) | ||
lazy val part1 = trails.sumBy(_.toSet.size) | ||
lazy val part2 = trails.sumBy(_.size) | ||
|
||
override def toString = grid.repr | ||
.map(xs => xs.map(x => (if x >= 0 then x else '.')).mkString) | ||
.mkString("\n") | ||
|
||
@main def main() = | ||
val input = readInput(this).mkString | ||
val ctx = Context(Grid(input).map(_.asDigit)) | ||
|
||
println(ctx) | ||
println(ctx.grid.size) | ||
println(ctx.part1) | ||
println(ctx.part2) |
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,44 @@ | ||
package `2024`.day10 | ||
|
||
val example1 = | ||
"""|0123 | ||
|1234 | ||
|8765 | ||
|9876""".stripMargin | ||
|
||
val example2 = | ||
"""|...0... | ||
|...1... | ||
|...2... | ||
|6543456 | ||
|7.....7 | ||
|8.....8 | ||
|9.....9""".stripMargin | ||
|
||
val example3 = | ||
"""|..90..9 | ||
|...1.98 | ||
|...2..7 | ||
|6543456 | ||
|765.987 | ||
|876.... | ||
|987....""".stripMargin | ||
|
||
val example4 = | ||
"""|10..9.. | ||
|2...8.. | ||
|3...7.. | ||
|4567654 | ||
|...8..3 | ||
|...9..2 | ||
|.....01""".stripMargin | ||
|
||
val example5 = | ||
"""|89010123 | ||
|78121874 | ||
|87430965 | ||
|96549874 | ||
|45678903 | ||
|32019012 | ||
|01329801 | ||
|10456732""".stripMargin |