Compare commits
3 Commits
4872056127
...
4445e63329
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4445e63329 | ||
|
|
94eb34f386 | ||
|
|
24936839a5 |
@@ -10,9 +10,7 @@ val solvers = Map[Int, Solver](
|
||||
5 -> Day05
|
||||
)
|
||||
|
||||
@main def main(day: String, input: String): Unit =
|
||||
solvers.get(day.toInt) match
|
||||
case Some(solver) =>
|
||||
def runSolver(solver: Solver, input: os.Path): Unit =
|
||||
val (timings, solution) = solver.run(input)
|
||||
sys.env.get("AOC_BENCH") match
|
||||
case Some(_) =>
|
||||
@@ -23,5 +21,15 @@ val solvers = Map[Int, Solver](
|
||||
println(f"Part 1: ${solution.p1}%15s ${timings.p1}%15s μs")
|
||||
println(f"Part 2: ${solution.p2}%15s ${timings.p2}%15s μs")
|
||||
|
||||
case None =>
|
||||
println(s"Day $day not solved")
|
||||
@main def main(day: String, input: String): Unit =
|
||||
val num = """(\d+)""".r
|
||||
day match
|
||||
case "all" =>
|
||||
solvers.foreach { case (day, solver) =>
|
||||
runSolver(solver, os.pwd / os.RelPath(input) / f"$day%02d.in")
|
||||
}
|
||||
case num(_) =>
|
||||
solvers.get(day.toInt) match
|
||||
case Some(solver) => runSolver(solver, os.pwd / os.RelPath(input))
|
||||
case None => println(s"Day $day not solved")
|
||||
case _ => println(day)
|
||||
|
||||
@@ -5,7 +5,7 @@ case class Timings(prep: Long, p1: Long, p2: Long)
|
||||
case class Solution(p1: Object, p2: Object)
|
||||
|
||||
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) =
|
||||
val start = System.nanoTime()
|
||||
|
||||
@@ -5,10 +5,9 @@ import dev.ctsk.aoc._
|
||||
object Day01 extends Solver(1):
|
||||
type Input = (Array[Int], Array[Int])
|
||||
|
||||
def pre(input: String): Input =
|
||||
io.Source
|
||||
.fromFile(input)
|
||||
.getLines()
|
||||
def pre(input: os.Path): Input =
|
||||
os.read
|
||||
.lines(input)
|
||||
.map { case s"$i $j" => (i.toInt, j.toInt) }
|
||||
.toArray
|
||||
.unzip match { case (left, right) => (left.sorted, right.sorted) }
|
||||
@@ -22,7 +21,7 @@ object Day01 extends Solver(1):
|
||||
val counts = right.groupMapReduce(identity)(identity)(_ + _)
|
||||
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 (p1_time, p1_solution) = timed { part1(pre_input) }
|
||||
val (p2_time, p2_solution) = timed { part2(pre_input) }
|
||||
|
||||
@@ -3,10 +3,9 @@ package dev.ctsk.aoc.days
|
||||
import dev.ctsk.aoc._
|
||||
|
||||
object Day02 extends Solver(2):
|
||||
def pre(input: String): List[List[Int]] =
|
||||
io.Source
|
||||
.fromFile(input)
|
||||
.getLines()
|
||||
def pre(input: os.Path): List[List[Int]] =
|
||||
os.read
|
||||
.lines(input)
|
||||
.map(line => line.split(" ").map(_.toInt).toList)
|
||||
.toList
|
||||
|
||||
@@ -25,7 +24,7 @@ object Day02 extends Solver(2):
|
||||
def part2(lists: List[List[Int]]): Int =
|
||||
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 (p1_time, p1_solution) = timed { part1(pre_input) }
|
||||
val (p2_time, p2_solution) = timed { part2(pre_input) }
|
||||
|
||||
@@ -24,8 +24,8 @@ object Day03 extends Solver(3):
|
||||
}
|
||||
._2
|
||||
|
||||
def run(input: String): (Timings, Solution) =
|
||||
val in = io.Source.fromFile(input).mkString
|
||||
def run(input: os.Path): (Timings, Solution) =
|
||||
val in = os.read(input)
|
||||
val (p1_time, p1_solution) = timed { part1(in) }
|
||||
val (p2_time, p2_solution) = timed { part2(in) }
|
||||
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
package dev.ctsk.aoc.days
|
||||
|
||||
import dev.ctsk.aoc._
|
||||
import scala.compiletime.ops.double
|
||||
import scala.reflect.ClassTag
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
import java.util.concurrent.ForkJoinPool
|
||||
|
||||
object Day04 extends Solver(4):
|
||||
private val XMAS = "XMAS".r
|
||||
private val REV_XMAS = "XMAS".reverse.r
|
||||
|
||||
def diagonals[A: ClassTag](m: Vector[Vector[A]]): Vector[Vector[A]] =
|
||||
def diagonals[A](m: Vector[Vector[A]]): Vector[Vector[A]] =
|
||||
val diagonals = Vector.fill(m.length + m(0).length - 1)(ArrayBuffer[A]())
|
||||
|
||||
for
|
||||
@@ -30,27 +27,19 @@ object Day04 extends Solver(4):
|
||||
lines.transpose,
|
||||
diagonals(lines),
|
||||
diagonals(lines.reverse)
|
||||
).flatMap(_.toList).map(count).sum
|
||||
).flatMap(_.map(count)).sum
|
||||
|
||||
def part2(grid: Vector[Vector[Char]]): Int =
|
||||
var count = 0
|
||||
|
||||
for
|
||||
(for
|
||||
i <- 1 until grid.length - 1
|
||||
j <- 1 until grid(i).length - 1
|
||||
if grid(i)(j) == 'A'
|
||||
& (grid(i - 1)(j - 1) + grid(i + 1)(j + 1) == 'S' + 'M')
|
||||
& (grid(i - 1)(j + 1) + grid(i + 1)(j - 1) == 'S' + 'M')
|
||||
do count += 1
|
||||
&& 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
|
||||
|
||||
count
|
||||
|
||||
def run(input: String): (Timings, Solution) =
|
||||
var in = io.Source
|
||||
.fromFile(input)
|
||||
.getLines()
|
||||
.map { line => line.toVector }
|
||||
.toVector
|
||||
def run(input: os.Path): (Timings, Solution) =
|
||||
var in = os.read.lines(input).map { line => line.toVector }.toVector
|
||||
|
||||
val (p1_time, p1_solution) = timed { part1(in) }
|
||||
val (p2_time, p2_solution) = timed { part2(in) }
|
||||
|
||||
@@ -4,9 +4,8 @@ import dev.ctsk.aoc._
|
||||
import scala.annotation.nowarn
|
||||
|
||||
object Day05 extends Solver(5):
|
||||
def pre(input: String) =
|
||||
val Array(rulesStr, updatesStr) =
|
||||
io.Source.fromFile(input).mkString.split("\n\n")
|
||||
def pre(input: os.Path) =
|
||||
val Array(rulesStr, updatesStr) = os.read(input).split("\n\n")
|
||||
|
||||
val rules = rulesStr.linesIterator
|
||||
.map(rule => {
|
||||
@@ -20,7 +19,7 @@ object Day05 extends Solver(5):
|
||||
|
||||
(rules, updates)
|
||||
|
||||
def run(input: String): (Timings, Solution) =
|
||||
def run(input: os.Path): (Timings, Solution) =
|
||||
val (pre_time, (rules, updates)) = timed { pre(input) }
|
||||
|
||||
@nowarn("msg=match may not be exhaustive")
|
||||
|
||||
@@ -4,7 +4,7 @@ import mill._, scalalib._
|
||||
|
||||
object aoc extends ScalaModule {
|
||||
def scalaVersion = "3.5.2"
|
||||
// def ivyDeps = Agg(
|
||||
// ivy"org.scala-lang.modules::scala-parallel-collections:1.0.4"
|
||||
// )
|
||||
def ivyDeps = Agg(
|
||||
ivy"com.lihaoyi::os-lib:0.11.3"
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user