Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day3 #11

Merged
merged 3 commits into from
Dec 3, 2024
Merged

Day3 #11

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Advent of Code 2024 in Scala 3 (first time using Scala). Completion is not guara
|-----|--------------|
| 1 | ⭐️⭐️ |
| 2 | ⭐️⭐️ |
| 3 | |
| 3 | ⭐️⭐️ |
| 4 | ❌ |
| 5 | ❌ |
| 6 | ❌ |
Expand Down
38 changes: 37 additions & 1 deletion src/main/scala/net/rohlandm/aoc24/application/Day3Solver.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package net.rohlandm.aoc24.application

import net.rohlandm.aoc24.application.Execution.ENABLED
import net.rohlandm.aoc24.domain.DaySolver

class Day3Solver extends DaySolver(3)
class Day3Solver extends DaySolver(3):

private val mulPattern = """mul\((\d+),(\d+)\)""".r
private val conditionalMulPattern = """mul\((\d+),(\d+)\)|do\(\)|don't\(\)""".r

override def solvePart1(input: List[String]): Option[Long] =
val instructions = input.flatMap(mulPattern.findAllIn(_))
Some(instructions.map(executeInstruction).sum)

override def solvePart2(input: List[String]): Option[Long] =
val instructions = input.flatMap(conditionalMulPattern.findAllIn(_))
var execution = ENABLED
var acc: Long = 0

instructions.foreach(instruction => {
val result = executeConditionally.apply(instruction, execution, acc)
acc = result._1
execution = result._2
})

Some(acc)

private val executeInstruction = (instruction: String) =>
val split = instruction.split("[(),]").toList
var execution = Execution.ENABLED
split.apply(1).toInt * split.apply(2).toInt

private val executeConditionally = (instruction: String, execution: Execution, acc: Long) =>
instruction match
case "do()" => (acc, Execution.ENABLED)
case "don't()" => (acc, Execution.DISABLED)
case mul if execution == ENABLED => (acc + executeInstruction.apply(mul), execution)
case _ => (acc, execution)

enum Execution:
case ENABLED, DISABLED
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import org.scalatest.funsuite.AnyFunSuiteLike
*/
class Day3SolverTest extends AnyFunSuiteLike:

private val testInput =
"""""".stripMargin.split("\n").toList
private val testInput1 =
"""xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))""".stripMargin.split("\n").toList

private val testInput2 =
"""xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))""".stripMargin.split("\n").toList

test("solvePart1 returns result"):
assert(Day3Solver().solvePart1(testInput) === None)
assert(Day3Solver().solvePart1(testInput1) === Some(161))

test("solvePart2 returns result"):
assert(Day3Solver().solvePart2(testInput) === None)
assert(Day3Solver().solvePart2(testInput2) === Some(48))
Loading