Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 2025/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//> using scala 3.7.4
//> using option -Wunused:all
//> using test.dep org.scalameta::munit::1.2.1
39 changes: 39 additions & 0 deletions 2025/src/day06.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package day06

import locations.Directory.currentDir
import inputs.Input.loadFileSync

@main def part1: Unit =
println(s"The solution is ${part1(loadInput())}")

@main def part2: Unit =
println(s"The solution is ${part2(loadInput())}")

def loadInput(): String = loadFileSync(s"$currentDir/../input/day06")

extension [A](xs: IterableOnce[A])
inline def splitBy(sep: A) =
val (b, cur) = (Vector.newBuilder[Vector[A]], Vector.newBuilder[A])
for e <- xs.iterator do
if e != sep then cur += e else { b += cur.result(); cur.clear() }
(b += cur.result()).result()

extension (xs: IterableOnce[(symbol: String, nums: IterableOnce[String])])
inline def calculate: Long = xs.iterator.collect {
case ("*", nums) => nums.iterator.map(_.toLong).product
case ("+", nums) => nums.iterator.map(_.toLong).sum
}.sum

def part1(input: String): Long = input.linesIterator.toVector
.map(_.trim.split(raw"\s+"))
.transpose
.iterator
.map { col => (col.last, col.view.init) }
.calculate

def part2(input: String): Long =
val lines = input.linesIterator.toVector
val ops = lines.last.split(raw"\s+").toVector
val xss = lines.init.transpose.map(_.mkString.trim).splitBy("")

(ops lazyZip xss).calculate
9 changes: 9 additions & 0 deletions 2025/src/inputs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package inputs

import scala.util.Using
import scala.io.Source

object Input:

def loadFileSync(path: String): String =
Using.resource(Source.fromFile(path))(_.mkString)
19 changes: 19 additions & 0 deletions 2025/src/locations.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package locations

import scala.quoted.*

object Directory:

/** The absolute path of the parent directory of the file that calls this method
* This is stable no matter which directory runs the program.
*/
inline def currentDir: String = ${ parentDirImpl }

private def parentDirImpl(using Quotes): Expr[String] =
// position of the call to `currentDir` in the source code
val position = quotes.reflect.Position.ofMacroExpansion
// get the path of the file calling this macro
val srcFilePath = position.sourceFile.getJPath.get
// get the parent of the path, which is the directory containing the file
val parentDir = srcFilePath.getParent().toAbsolutePath
Expr(parentDir.toString) // convert the String to Expr[String]