Skip to content

Commit

Permalink
feat(2024): day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Dec 5, 2024
1 parent 821126b commit 4a190d9
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 2024/day05.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package `2024`.day05

import prelude.*

def middle[A](xs: IndexedSeq[A]) = xs(xs.size / 2)

@main def main() =
val (rawRules, rawPages) = readInput(this).mkString
.split("\n\n")
.map(_.split("\n")) match { case Array(a, b) => (a, b) }

given ord: Ordering[Int] = rawRules
.collect { case s"$a|$b" => a.toInt -> b.toInt }
.groupMap(_._1)(_._2)
.view
.mapValues(_.toSet)
.toMap
.withDefaultValue(Set())
|> (deps => Ordering.fromLessThan(deps(_)(_)))

val pages = rawPages.map(_.split(",").map(_.toInt).toVector).toVector

val (wrongs, rights) = pages.partitionMap { page =>
val sort = page.sorted
if page == sort then Right(sort) else Left(sort)
}
println(rights.sumBy(middle))
println(wrongs.sumBy(middle))

0 comments on commit 4a190d9

Please sign in to comment.