Switch to os-lib

This commit is contained in:
Christian
2024-12-05 15:30:22 +01:00
parent 24936839a5
commit 94eb34f386
7 changed files with 17 additions and 24 deletions

View File

@@ -13,7 +13,7 @@ val solvers = Map[Int, Solver](
@main def main(day: String, input: String): Unit = @main def main(day: String, input: String): Unit =
solvers.get(day.toInt) match solvers.get(day.toInt) match
case Some(solver) => case Some(solver) =>
val (timings, solution) = solver.run(input) val (timings, solution) = solver.run(os.pwd / input)
sys.env.get("AOC_BENCH") match sys.env.get("AOC_BENCH") match
case Some(_) => case Some(_) =>
println(solution.p1) println(solution.p1)

View File

@@ -5,7 +5,7 @@ case class Timings(prep: Long, p1: Long, p2: Long)
case class Solution(p1: Object, p2: Object) case class Solution(p1: Object, p2: Object)
abstract class Solver(day: Int): abstract class Solver(day: Int):
def run(input: String): (Timings, Solution) def run(input: os.Path): (Timings, Solution)
def timed[A](solution: => A): (Long, A) = def timed[A](solution: => A): (Long, A) =
val start = System.nanoTime() val start = System.nanoTime()

View File

@@ -5,10 +5,9 @@ import dev.ctsk.aoc._
object Day01 extends Solver(1): object Day01 extends Solver(1):
type Input = (Array[Int], Array[Int]) type Input = (Array[Int], Array[Int])
def pre(input: String): Input = def pre(input: os.Path): Input =
io.Source os.read
.fromFile(input) .lines(input)
.getLines()
.map { case s"$i $j" => (i.toInt, j.toInt) } .map { case s"$i $j" => (i.toInt, j.toInt) }
.toArray .toArray
.unzip match { case (left, right) => (left.sorted, right.sorted) } .unzip match { case (left, right) => (left.sorted, right.sorted) }
@@ -22,7 +21,7 @@ object Day01 extends Solver(1):
val counts = right.groupMapReduce(identity)(identity)(_ + _) val counts = right.groupMapReduce(identity)(identity)(_ + _)
left.map(n => counts.getOrElse(n, 0)).sum left.map(n => counts.getOrElse(n, 0)).sum
def run(input: String): (Timings, Solution) = def run(input: os.Path): (Timings, Solution) =
val (pre_time, pre_input) = timed { pre(input) } val (pre_time, pre_input) = timed { pre(input) }
val (p1_time, p1_solution) = timed { part1(pre_input) } val (p1_time, p1_solution) = timed { part1(pre_input) }
val (p2_time, p2_solution) = timed { part2(pre_input) } val (p2_time, p2_solution) = timed { part2(pre_input) }

View File

@@ -3,10 +3,9 @@ package dev.ctsk.aoc.days
import dev.ctsk.aoc._ import dev.ctsk.aoc._
object Day02 extends Solver(2): object Day02 extends Solver(2):
def pre(input: String): List[List[Int]] = def pre(input: os.Path): List[List[Int]] =
io.Source os.read
.fromFile(input) .lines(input)
.getLines()
.map(line => line.split(" ").map(_.toInt).toList) .map(line => line.split(" ").map(_.toInt).toList)
.toList .toList
@@ -25,7 +24,7 @@ object Day02 extends Solver(2):
def part2(lists: List[List[Int]]): Int = def part2(lists: List[List[Int]]): Int =
lists.count(list => safeWithGap(list) || safeWithGap(list.reverse)) lists.count(list => safeWithGap(list) || safeWithGap(list.reverse))
def run(input: String): (Timings, Solution) = def run(input: os.Path): (Timings, Solution) =
val (pre_time, pre_input) = timed { pre(input) } val (pre_time, pre_input) = timed { pre(input) }
val (p1_time, p1_solution) = timed { part1(pre_input) } val (p1_time, p1_solution) = timed { part1(pre_input) }
val (p2_time, p2_solution) = timed { part2(pre_input) } val (p2_time, p2_solution) = timed { part2(pre_input) }

View File

@@ -24,8 +24,8 @@ object Day03 extends Solver(3):
} }
._2 ._2
def run(input: String): (Timings, Solution) = def run(input: os.Path): (Timings, Solution) =
val in = io.Source.fromFile(input).mkString val in = os.read(input)
val (p1_time, p1_solution) = timed { part1(in) } val (p1_time, p1_solution) = timed { part1(in) }
val (p2_time, p2_solution) = timed { part2(in) } val (p2_time, p2_solution) = timed { part2(in) }

View File

@@ -38,12 +38,8 @@ object Day04 extends Solver(4):
&& grid(i - 1)(j + 1) + grid(i + 1)(j - 1) == 'S' + 'M' && grid(i - 1)(j + 1) + grid(i + 1)(j - 1) == 'S' + 'M'
yield ()).length yield ()).length
def run(input: String): (Timings, Solution) = def run(input: os.Path): (Timings, Solution) =
var in = io.Source var in = os.read.lines(input).map { line => line.toVector }.toVector
.fromFile(input)
.getLines()
.map { line => line.toVector }
.toVector
val (p1_time, p1_solution) = timed { part1(in) } val (p1_time, p1_solution) = timed { part1(in) }
val (p2_time, p2_solution) = timed { part2(in) } val (p2_time, p2_solution) = timed { part2(in) }

View File

@@ -4,9 +4,8 @@ import dev.ctsk.aoc._
import scala.annotation.nowarn import scala.annotation.nowarn
object Day05 extends Solver(5): object Day05 extends Solver(5):
def pre(input: String) = def pre(input: os.Path) =
val Array(rulesStr, updatesStr) = val Array(rulesStr, updatesStr) = os.read(input).split("\n\n")
io.Source.fromFile(input).mkString.split("\n\n")
val rules = rulesStr.linesIterator val rules = rulesStr.linesIterator
.map(rule => { .map(rule => {
@@ -20,7 +19,7 @@ object Day05 extends Solver(5):
(rules, updates) (rules, updates)
def run(input: String): (Timings, Solution) = def run(input: os.Path): (Timings, Solution) =
val (pre_time, (rules, updates)) = timed { pre(input) } val (pre_time, (rules, updates)) = timed { pre(input) }
@nowarn("msg=match may not be exhaustive") @nowarn("msg=match may not be exhaustive")