File tree Expand file tree Collapse file tree 1 file changed +6
-9
lines changed
src/main/kotlin/me/peckb/aoc/_2024/calendar/day03 Expand file tree Collapse file tree 1 file changed +6
-9
lines changed Original file line number Diff line number Diff line change @@ -8,17 +8,17 @@ class Day03 @Inject constructor(
88) {
99 fun partOne (filename : String ) = generatorFactory.forFile(filename).read { lines ->
1010 lines.sumOf { line ->
11- val matches = " mul\\ (\\ d+, \\ d+\\ )" .toRegex().findAll(line)
11+ val matches = " mul\\ ((?<d1> \\ d+),(?<d2> \\ d+) \\ )" .toRegex().findAll(line)
1212
13- matches.sumOf { match -> match.value .getMultiplicationResult() }
13+ matches.sumOf { it .getMultiplicationResult() }
1414 }
1515 }
1616
1717 fun partTwo (filename : String ) = generatorFactory.forFile(filename).read { lines ->
1818 var enabled = true
1919
2020 lines.sumOf { line ->
21- val mulMatches = " mul\\ (\\ d+, \\ d+\\ )" .toRegex().findAll(line)
21+ val mulMatches = " mul\\ ((?<d1> \\ d+),(?<d2> \\ d+) \\ )" .toRegex().findAll(line)
2222 val doMatches = " do\\ (\\ )" .toRegex().findAll(line)
2323 val dontMatches = " don\' t\\ (\\ )" .toRegex().findAll(line)
2424
@@ -27,19 +27,16 @@ class Day03 @Inject constructor(
2727 var count = 0L
2828
2929 sortedMatches.forEach { match ->
30- val value = match.value
31-
32- when (value.take(3 )) {
30+ when (match.value.take(3 )) {
3331 " do(" -> enabled = true
3432 " don" -> enabled = false
35- " mul" -> if (enabled) { count + = value .getMultiplicationResult() }
33+ " mul" -> if (enabled) { count + = match .getMultiplicationResult() }
3634 }
3735 }
3836
3937 count
4038 }
4139 }
4240
43- private fun String.getMultiplicationResult () =
44- this .drop(4 ).dropLast(1 ).split(" ," ).map { it.toInt() }.reduce(Int ::times)
41+ private fun MatchResult.getMultiplicationResult () = groupValues[1 ].toInt() * groupValues[2 ].toInt()
4542}
You can’t perform that action at this time.
0 commit comments