Day 01
This commit is contained in:
32
.gitignore
vendored
Normal file
32
.gitignore
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
# macOS
|
||||
.DS_Store
|
||||
|
||||
# sbt specific
|
||||
dist/*
|
||||
target/
|
||||
lib_managed/
|
||||
src_managed/
|
||||
project/boot/
|
||||
project/plugins/project/
|
||||
project/local-plugins.sbt
|
||||
.history
|
||||
.ensime
|
||||
.ensime_cache/
|
||||
.sbt-scripted/
|
||||
local.sbt
|
||||
|
||||
# Bloop
|
||||
.bsp
|
||||
|
||||
# VS Code
|
||||
.vscode/
|
||||
|
||||
# Metals
|
||||
.bloop/
|
||||
.metals/
|
||||
metals.sbt
|
||||
|
||||
# IDEA
|
||||
.idea
|
||||
.idea_modules
|
||||
/.worksheet/
|
||||
2
.scalafmt.conf
Normal file
2
.scalafmt.conf
Normal file
@@ -0,0 +1,2 @@
|
||||
version = "3.7.15"
|
||||
runner.dialect = scala3
|
||||
10
build.sbt
Normal file
10
build.sbt
Normal file
@@ -0,0 +1,10 @@
|
||||
val scala3Version = "3.5.2"
|
||||
|
||||
lazy val root = project
|
||||
.in(file("."))
|
||||
.settings(
|
||||
name := "aoc-2024",
|
||||
version := "0.1.0-SNAPSHOT",
|
||||
scalaVersion := scala3Version,
|
||||
libraryDependencies += "org.scalameta" %% "munit" % "1.0.0" % Test
|
||||
)
|
||||
1000
data/01.in
Normal file
1000
data/01.in
Normal file
File diff suppressed because it is too large
Load Diff
1
project/build.properties
Normal file
1
project/build.properties
Normal file
@@ -0,0 +1 @@
|
||||
sbt.version=1.10.5
|
||||
18
src/main/scala/Main.scala
Normal file
18
src/main/scala/Main.scala
Normal file
@@ -0,0 +1,18 @@
|
||||
package dev.ctsk.aoc
|
||||
|
||||
import dev.ctsk.aoc.days._
|
||||
|
||||
val solvers = Map[Int, Solver](
|
||||
1 -> Day01
|
||||
)
|
||||
|
||||
@main def main(day: String, input: String): Unit =
|
||||
solvers.get(day.toInt) match
|
||||
case Some(solver) =>
|
||||
val (timings, solution) = solver.run(input)
|
||||
println(f"Preprocessing: ${timings.prep}%24s μs")
|
||||
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")
|
||||
15
src/main/scala/Solution.scala
Normal file
15
src/main/scala/Solution.scala
Normal file
@@ -0,0 +1,15 @@
|
||||
package dev.ctsk.aoc
|
||||
|
||||
case class Timings(prep: Long, p1: Long, p2: Long)
|
||||
|
||||
case class Solution(p1: String, p2: String)
|
||||
|
||||
abstract class Solver(day: Int):
|
||||
def run(input: String): (Timings, Solution)
|
||||
|
||||
def timed[A](solution: => A): (Long, A) =
|
||||
val start = System.nanoTime()
|
||||
val result = solution
|
||||
val end = System.nanoTime()
|
||||
|
||||
((end - start) / 1000, result)
|
||||
30
src/main/scala/days/Day01.scala
Normal file
30
src/main/scala/days/Day01.scala
Normal file
@@ -0,0 +1,30 @@
|
||||
package dev.ctsk.aoc.days
|
||||
|
||||
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()
|
||||
.map { case s"$i $j" => (i.toInt, j.toInt) }
|
||||
.toArray
|
||||
.unzip match { case (left, right) => (left.sorted, right.sorted) }
|
||||
|
||||
def part1(input: Input): String =
|
||||
input match
|
||||
case (left, right) => left.zip(right).map(_ - _).map(_.abs).sum.toString
|
||||
|
||||
def part2(input: Input): String =
|
||||
val (left, right) = input;
|
||||
val counts = right.groupBy(identity).mapValues(_.length).toMap
|
||||
left.map(n => n * counts.getOrElse(n, 0)).sum.toString
|
||||
|
||||
def run(input: String): (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) }
|
||||
|
||||
(Timings(pre_time, p1_time, p2_time), Solution(p1_solution, p2_solution))
|
||||
9
src/test/scala/MySuite.scala
Normal file
9
src/test/scala/MySuite.scala
Normal file
@@ -0,0 +1,9 @@
|
||||
// For more information on writing tests, see
|
||||
// https://scalameta.org/munit/docs/getting-started.html
|
||||
class MySuite extends munit.FunSuite {
|
||||
test("example test that succeeds") {
|
||||
val obtained = 42
|
||||
val expected = 42
|
||||
assertEquals(obtained, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user