-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version = 1.10.5 | ||
sbt.version = 1.10.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Large diffs are not rendered by default.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
scala2/src/main/scala/jurisk/adventofcode/y2024/Advent03.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
scala2/src/test/scala/jurisk/adventofcode/y2024/Advent03Spec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version = 1.10.5 | ||
sbt.version = 1.10.6 |