Skip to content

Commit

Permalink
Solve day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Dec 3, 2024
1 parent 2e9d191 commit fd7b6e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2024/src/main/kotlin/aoc/year2024/Day3.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package aoc.year2024

import aoc.library.Puzzle

object Day3 : Puzzle<Long, Long>(day = 3) {

override fun solvePart1(input: String): Long = runMultiplications(input)

override fun solvePart2(input: String): Long = runMultiplications(clean(input))

private fun clean(input: String): String {
val firstDont = input.indexOf("don't()")
if (firstDont == -1) return input
val firstDo = input.indexOf("do()", startIndex = firstDont)
if (firstDo == -1) return input
return clean(input.take(firstDont) + input.drop(firstDo + 4))
}

private fun runMultiplications(input: String): Long {
val regex = """mul\((\d+),(\d+)\)""".toRegex()
return regex.findAll(input).map {
it.groupValues[1].toLong() * it.groupValues[2].toLong()
}.sum()
}
}
29 changes: 29 additions & 0 deletions 2024/src/test/kotlin/aoc/year2024/Day3Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package aoc.year2024

import aoc.library.solvePart1
import aoc.library.solvePart2
import io.kotest.matchers.longs.shouldBeExactly
import org.junit.jupiter.api.Test

class Day3Test {

@Test
fun part1TestInput() {
Day3.solvePart1("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))") shouldBeExactly 161
}

@Test
fun part1() {
Day3.solvePart1() shouldBeExactly 175015740
}

@Test
fun part2TestInput() {
Day3.solvePart2("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))") shouldBeExactly 48
}

@Test
fun part2() {
Day3.solvePart2() shouldBeExactly 112272912
}
}

0 comments on commit fd7b6e6

Please sign in to comment.