Skip to content

Commit

Permalink
feat(2024/10): 2024 day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Dec 10, 2024
1 parent 1282e1f commit 22d33e8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 2024/day10.scala
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)
44 changes: 44 additions & 0 deletions 2024/day10.test.scala
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

0 comments on commit 22d33e8

Please sign in to comment.