Fix absolute paths

This commit is contained in:
ctsk
2024-12-05 19:18:10 +01:00
parent 4445e63329
commit 62d40ed6ca
8 changed files with 23 additions and 11 deletions

View File

@@ -26,10 +26,10 @@ def runSolver(solver: Solver, input: os.Path): Unit =
day match day match
case "all" => case "all" =>
solvers.foreach { case (day, solver) => solvers.foreach { case (day, solver) =>
runSolver(solver, os.pwd / os.RelPath(input) / f"$day%02d.in") runSolver(solver, os.Path(input, os.pwd) / f"$day%02d.in")
} }
case num(_) => case num(_) =>
solvers.get(day.toInt) match solvers.get(day.toInt) match
case Some(solver) => runSolver(solver, os.pwd / os.RelPath(input)) case Some(solver) => runSolver(solver, os.Path(input, os.pwd))
case None => println(s"Day $day not solved") case None => println(s"Day $day not solved")
case _ => println(day) case _ => println(day)

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: os.Path): (Timings, Solution) def run(input: os.ReadablePath): (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

@@ -0,0 +1,12 @@
package dev.ctsk.aoc
import scala.collection
val NAT_REGEX = """(\d+)""".r
val INT_REGEX = """-?(\d+)""".r
def naturals(string: CharSequence): Vector[Long] =
NAT_REGEX.findAllIn(string).map(_.toLong).toVector
def integers(string: CharSequence): Vector[Long] =
INT_REGEX.findAllIn(string).map(_.toLong).toVector

View File

@@ -5,7 +5,7 @@ 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: os.Path): Input = def pre(input: os.ReadablePath): Input =
os.read os.read
.lines(input) .lines(input)
.map { case s"$i $j" => (i.toInt, j.toInt) } .map { case s"$i $j" => (i.toInt, j.toInt) }
@@ -21,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: os.Path): (Timings, Solution) = def run(input: os.ReadablePath): (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,7 +3,7 @@ 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: os.Path): List[List[Int]] = def pre(input: os.ReadablePath): List[List[Int]] =
os.read os.read
.lines(input) .lines(input)
.map(line => line.split(" ").map(_.toInt).toList) .map(line => line.split(" ").map(_.toInt).toList)
@@ -24,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: os.Path): (Timings, Solution) = def run(input: os.ReadablePath): (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,7 +24,7 @@ object Day03 extends Solver(3):
} }
._2 ._2
def run(input: os.Path): (Timings, Solution) = def run(input: os.ReadablePath): (Timings, Solution) =
val in = os.read(input) 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,7 +38,7 @@ 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: os.Path): (Timings, Solution) = def run(input: os.ReadablePath): (Timings, Solution) =
var in = os.read.lines(input).map { line => line.toVector }.toVector var in = os.read.lines(input).map { line => line.toVector }.toVector
val (p1_time, p1_solution) = timed { part1(in) } val (p1_time, p1_solution) = timed { part1(in) }

View File

@@ -4,7 +4,7 @@ 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: os.Path) = def pre(input: os.ReadablePath) =
val Array(rulesStr, updatesStr) = os.read(input).split("\n\n") val Array(rulesStr, updatesStr) = os.read(input).split("\n\n")
val rules = rulesStr.linesIterator val rules = rulesStr.linesIterator
@@ -19,7 +19,7 @@ object Day05 extends Solver(5):
(rules, updates) (rules, updates)
def run(input: os.Path): (Timings, Solution) = def run(input: os.ReadablePath): (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")