generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
2e9d191
commit fd7b6e6
Showing
2 changed files
with
54 additions
and
0 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 |
---|---|---|
@@ -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() | ||
} | ||
} |
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,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 | ||
} | ||
} |