Day 05
This commit is contained in:
@@ -6,7 +6,8 @@ val solvers = Map[Int, Solver](
|
||||
1 -> Day01,
|
||||
2 -> Day02,
|
||||
3 -> Day03,
|
||||
4 -> Day04
|
||||
4 -> Day04,
|
||||
5 -> Day05
|
||||
)
|
||||
|
||||
@main def main(day: String, input: String): Unit =
|
||||
|
||||
39
aoc/src/dev/ctsk/aoc/days/Day05.scala
Normal file
39
aoc/src/dev/ctsk/aoc/days/Day05.scala
Normal file
@@ -0,0 +1,39 @@
|
||||
package dev.ctsk.aoc.days
|
||||
|
||||
import dev.ctsk.aoc._
|
||||
|
||||
object Day05 extends Solver(5):
|
||||
def pre(input: String): (Set[(Int, Int)], Vector[Vector[Int]]) =
|
||||
val Array(rulesStr, updatesStr) =
|
||||
io.Source.fromFile(input).mkString.split("\n\n")
|
||||
|
||||
val rules = rulesStr.linesIterator
|
||||
.map(rule => {
|
||||
val Array(a, b) = rule.split("""\|""")
|
||||
(a.toInt, b.toInt)
|
||||
})
|
||||
.toSet
|
||||
|
||||
val updates =
|
||||
updatesStr.linesIterator.map(_.split(",").map(_.toInt).toVector).toVector
|
||||
|
||||
(rules, updates)
|
||||
|
||||
def run(input: String): (Timings, Solution) =
|
||||
val (pre_time, (rules, updates)) = timed { pre(input) }
|
||||
|
||||
def isOrdered(u: Vector[Int]): Boolean =
|
||||
!u.combinations(2).exists { case Seq(a, b) =>
|
||||
rules.contains((b, a))
|
||||
}
|
||||
|
||||
def findMiddle(u: Vector[Int]): Int =
|
||||
u.find(e =>
|
||||
u.count(rules.contains(_, e)) <= u.length / 2
|
||||
&& u.count(rules.contains(e, _)) <= u.length / 2
|
||||
).get
|
||||
|
||||
val p1 = timed { updates.filter(isOrdered).map(u => u(u.length / 2)).sum }
|
||||
val p2 = timed { updates.filterNot(isOrdered).map(findMiddle).sum }
|
||||
|
||||
(Timings(pre_time, p1._1, p2._1), Solution(Int.box(p1._2), Int.box(p2._2)))
|
||||
2
data/05.ans
Normal file
2
data/05.ans
Normal file
@@ -0,0 +1,2 @@
|
||||
4637
|
||||
6370
|
||||
1367
data/05.in
Normal file
1367
data/05.in
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user