Skip to content

Commit 80de7ee

Browse files
committed
feat: 2025 day 06 source code
1 parent 39a8870 commit 80de7ee

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

2025/project.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//> using scala 3.7.4
2+
//> using option -Wunused:all
3+
//> using test.dep org.scalameta::munit::1.2.1

2025/src/day06.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package day06
2+
3+
import locations.Directory.currentDir
4+
import inputs.Input.loadFileSync
5+
6+
@main def part1: Unit =
7+
println(s"The solution is ${part1(loadInput())}")
8+
9+
@main def part2: Unit =
10+
println(s"The solution is ${part2(loadInput())}")
11+
12+
def loadInput(): String = loadFileSync(s"$currentDir/../input/day06")
13+
14+
extension [A](xs: IterableOnce[A])
15+
inline def splitBy(sep: A) =
16+
val (b, cur) = (Vector.newBuilder[Vector[A]], Vector.newBuilder[A])
17+
for e <- xs.iterator do
18+
if e != sep then cur += e else { b += cur.result(); cur.clear() }
19+
(b += cur.result()).result()
20+
21+
extension (xs: IterableOnce[(symbol: String, nums: IterableOnce[String])])
22+
inline def calculate: Long = xs.iterator.collect {
23+
case ("*", nums) => nums.iterator.map(_.toLong).product
24+
case ("+", nums) => nums.iterator.map(_.toLong).sum
25+
}.sum
26+
27+
def part1(input: String): Long = input.linesIterator.toVector
28+
.map(_.trim.split(raw"\s+"))
29+
.transpose
30+
.iterator
31+
.map { col => (col.last, col.view.init) }
32+
.calculate
33+
34+
def part2(input: String): Long =
35+
val lines = input.linesIterator.toVector
36+
val ops = lines.last.split(raw"\s+").toVector
37+
val xss = lines.init.transpose.map(_.mkString.trim).splitBy("")
38+
39+
(ops lazyZip xss).calculate

2025/src/inputs.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package inputs
2+
3+
import scala.util.Using
4+
import scala.io.Source
5+
6+
object Input:
7+
8+
def loadFileSync(path: String): String =
9+
Using.resource(Source.fromFile(path))(_.mkString)

2025/src/locations.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package locations
2+
3+
import scala.quoted.*
4+
5+
object Directory:
6+
7+
/** The absolute path of the parent directory of the file that calls this method
8+
* This is stable no matter which directory runs the program.
9+
*/
10+
inline def currentDir: String = ${ parentDirImpl }
11+
12+
private def parentDirImpl(using Quotes): Expr[String] =
13+
// position of the call to `currentDir` in the source code
14+
val position = quotes.reflect.Position.ofMacroExpansion
15+
// get the path of the file calling this macro
16+
val srcFilePath = position.sourceFile.getJPath.get
17+
// get the parent of the path, which is the directory containing the file
18+
val parentDir = srcFilePath.getParent().toAbsolutePath
19+
Expr(parentDir.toString) // convert the String to Expr[String]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ See earlier editions:
99
- [2021](/2021/README.md)
1010
- [2022](/2022/README.md)
1111
- [2023](/2023/README.md)
12+
- [2024](/2024/README.md)
13+
- [2025](/2025/README.md)
1214

1315
## Website
1416

0 commit comments

Comments
 (0)