Skip to content

Commit

Permalink
2024-03 Scala 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 3, 2024
1 parent fcca447 commit 6cf0073
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion scala2/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.10.5
sbt.version = 1.10.6
1 change: 1 addition & 0 deletions scala2/src/main/resources/2024/03-test-00.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
1 change: 1 addition & 0 deletions scala2/src/main/resources/2024/03-test-01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
6 changes: 6 additions & 0 deletions scala2/src/main/resources/2024/03.txt

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions scala2/src/main/scala/jurisk/adventofcode/y2024/Advent03.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package jurisk.adventofcode.y2024

import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import mouse.all.booleanSyntaxMouse

import scala.util.matching.Regex

object Advent03 {
type Input = String

private val Number = """(\d+)""".r
private val Mul = s"""mul\\($Number,$Number\\)""".r
private val Do = """do\(\)""".r
private val Dont = """don't\(\)""".r

def solve(options: Regex)(data: Input): Int = {
val matches = options.findAllIn(data)
val (result, _) = matches.foldLeft((0, true)) {
case ((result, active), m) =>
m.trim match {
case Mul(a, b) =>
(result + active.fold(a.toInt * b.toInt, 0), active)
case Do() =>
(result, true)
case Dont() =>
(result, false)
case _ =>
m.fail
}
}

result
}

def part1: Input => Int = solve(Mul)
def part2: Input => Int = solve(List(Mul, Do, Dont).mkString("|").r)

def fileName(suffix: String): String =
s"2024/03$suffix.txt"

def main(args: Array[String]): Unit = {
val realData: Input = readFileText(fileName(""))

println(s"Part 1: ${part1(realData)}")
println(s"Part 2: ${part2(realData)}")
}
}
32 changes: 32 additions & 0 deletions scala2/src/test/scala/jurisk/adventofcode/y2024/Advent03Spec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package jurisk.adventofcode.y2024

import Advent03._
import jurisk.utils.FileInput.readFileText
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers._

class Advent03Spec extends AnyFreeSpec {
private def testData00 = readFileText(fileName("-test-00"))
private def testData01 = readFileText(fileName("-test-01"))
private def realData = readFileText(fileName(""))

"part 1" - {
"test" in {
part1(testData00) shouldEqual 2 * 4 + 5 * 5 + 11 * 8 + 8 * 5
}

"real" in {
part1(realData) shouldEqual 187825547
}
}

"part 2" - {
"test" in {
part2(testData01) shouldEqual 2 * 4 + 8 * 5
}

"real" in {
part2(realData) shouldEqual 85508223
}
}
}
4 changes: 2 additions & 2 deletions scala3/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ lazy val root = project
),
scalaVersion := "3.5.2",
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-effect" % "3.5.5",
"org.scalameta" %% "munit" % "1.0.2" % Test,
"org.typelevel" %% "cats-effect" % "3.5.7",
"org.scalameta" %% "munit" % "1.0.3" % Test,
),
)
2 changes: 1 addition & 1 deletion scala3/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.10.5
sbt.version = 1.10.6

0 comments on commit 6cf0073

Please sign in to comment.