diff --git a/README.md b/README.md index b70aa4d..f705898 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ | 2021 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | ★ | ★ | | | | | | | | | | | | 26 | | 2022 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | | | | | | | ☆ | | | | ☆ | 28 | | 2023 | ★ | ★ | ★ | ★ | ☆ | ★ | ☆ | | | | | | | | | | | | | | | | | | | 12 | -| 2024 | ★ | ★ | ☆ | | | | | | | | | | | | | | | | | | | | | | | 5 | +| 2024 | ★ | ★ | ★ | | | | | | | | | | | | | | | | | | | | | | | 6 | ## 🛷 How to run @@ -164,7 +164,7 @@ e.g. `HandyHaversacks`)* | | 7 | [Camel Cards](https://adventofcode.com/2023/day/7) | [[Code](src/main/kotlin/adventofcode/year2023/Day07CamelCards.kt)] [[Test](src/test/kotlin/adventofcode/year2023/Day07CamelCardsSpec.kt)] | `250474325` | | | [**2024**](https://adventofcode.com/2024) | 1 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | [[Code](src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day01HistorianHysteriaSpec.kt)] | `1970720` | `17191599` | | | 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [[Code](src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day02RedNosedReportsSpec.kt)] | `572` | `612` | -| | 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [[Code](src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt)] | `174960292` | | +| | 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [[Code](src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt)] | `174960292` | `56275602` | ## 🕯️ Useful commands diff --git a/src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt b/src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt index 4c0acd8..8b8e494 100644 --- a/src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt +++ b/src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt @@ -4,13 +4,23 @@ import adventofcode.Puzzle import adventofcode.PuzzleInput class Day03MullItOver(customInput: PuzzleInput? = null) : Puzzle(customInput) { - override fun partOne() = - mulRegex - .findAll(input) - .map { it.destructured } - .sumOf { (x, y) -> x.toInt() * y.toInt() } + override fun partOne() = input.sumOfMultiplications() + + override fun partTwo() = + input + .split(doRegex) + .map { doPart -> doPart.split(dontRegex).first() } + .sumOf { it.sumOfMultiplications() } companion object { private val mulRegex = """mul\((\d{1,3}),(\d{1,3})\)""".toRegex() + private val doRegex = """do\(\)""".toRegex() + private val dontRegex = """don't\(\)""".toRegex() + + private fun String.sumOfMultiplications() = + mulRegex + .findAll(this) + .map { it.destructured } + .sumOf { (x, y) -> x.toInt() * y.toInt() } } } diff --git a/src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt b/src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt index d738c9e..a1222f5 100644 --- a/src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt +++ b/src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt @@ -2,4 +2,7 @@ package adventofcode.year2024 import adventofcode.PuzzleBaseSpec -class Day03MullItOverSpec : PuzzleBaseSpec(161) +class Day03MullItOverSpec : PuzzleBaseSpec( + listOf("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))" to 161), + listOf("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" to 48), +) diff --git a/src/test/resources/inputs/year2024/day03.txt b/src/test/resources/inputs/year2024/day03.txt deleted file mode 100644 index 2e1a90a..0000000 --- a/src/test/resources/inputs/year2024/day03.txt +++ /dev/null @@ -1 +0,0 @@ -xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) \ No newline at end of file