File tree Expand file tree Collapse file tree 5 files changed +84
-0
lines changed
main/kotlin/me/peckb/aoc/_2024/calendar/day03
test/kotlin/me/peckb/aoc/_2024 Expand file tree Collapse file tree 5 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,10 @@ as the value which passed the given day/phase combination
100100* Day 20
101101 * A good reverse engineering problem, where you need to examine the input to understand the problem; without the reverse engineering logic being too hard to spot
102102
103+ #### 2024
104+ * Day 03
105+ * Regex shining today! Sorting by the match location made the single list of all matches easy to scan through.
106+
103107### Interesting approaches:
104108
105109#### 2015
Original file line number Diff line number Diff line change 1+ package me.peckb.aoc._2024.calendar.day03
2+
3+ import javax.inject.Inject
4+ import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
6+ class Day03 @Inject constructor(
7+ private val generatorFactory : InputGeneratorFactory ,
8+ ) {
9+ fun partOne (filename : String ) = generatorFactory.forFile(filename).read { lines ->
10+ lines.sumOf { line ->
11+ val matches = " mul\\ (\\ d+,\\ d+\\ )" .toRegex().findAll(line)
12+
13+ matches.sumOf { match -> match.value.getMultiplicationResult() }
14+ }
15+ }
16+
17+ fun partTwo (filename : String ) = generatorFactory.forFile(filename).read { lines ->
18+ var enabled = true
19+
20+ lines.sumOf { line ->
21+ val mulMatches = " mul\\ (\\ d+,\\ d+\\ )" .toRegex().findAll(line)
22+ val doMatches = " do\\ (\\ )" .toRegex().findAll(line)
23+ val dontMatches = " don\' t\\ (\\ )" .toRegex().findAll(line)
24+
25+ val sortedMatches = (mulMatches + doMatches + dontMatches).sortedBy { it.range.first }
26+
27+ var count = 0L
28+
29+ sortedMatches.forEach { match ->
30+ val value = match.value
31+
32+ when (value.take(3 )) {
33+ " do(" -> enabled = true
34+ " don" -> enabled = false
35+ " mul" -> if (enabled) { count + = value.getMultiplicationResult() }
36+ }
37+ }
38+
39+ count
40+ }
41+ }
42+
43+ private fun String.getMultiplicationResult () =
44+ this .drop(4 ).dropLast(1 ).split(" ," ).map { it.toInt() }.reduce(Int ::times)
45+ }
Original file line number Diff line number Diff line change 1+ ## [ Day 3: Mull It Over] ( https://adventofcode.com/2024/day/3 )
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package me.peckb.aoc._2024
22
33import dagger.Component
44import me.peckb.aoc._2024.calendar.day02.Day02Test
5+ import me.peckb.aoc._2024.calendar.day03.Day03Test
56import javax.inject.Singleton
67import me.peckb.aoc.DayComponent
78import me.peckb.aoc.InputModule
@@ -12,4 +13,5 @@ import me.peckb.aoc._2024.calendar.day01.Day01Test
1213internal interface TestDayComponent : DayComponent {
1314 fun inject (day01Test : Day01Test )
1415 fun inject (day02Test : Day02Test )
16+ fun inject (day03Test : Day03Test )
1517}
Original file line number Diff line number Diff line change 1+ package me.peckb.aoc._2024.calendar.day03
2+
3+ import javax.inject.Inject
4+
5+ import me.peckb.aoc._2024.DaggerTestDayComponent
6+ import org.junit.jupiter.api.Assertions.assertEquals
7+ import org.junit.jupiter.api.BeforeEach
8+ import org.junit.jupiter.api.Test
9+
10+ internal class Day03Test {
11+ @Inject
12+ lateinit var day03: Day03
13+
14+ @BeforeEach
15+ fun setup () {
16+ DaggerTestDayComponent .create().inject(this )
17+ }
18+
19+ @Test
20+ fun testDay03PartOne () {
21+ assertEquals(173517243 , day03.partOne(DAY_03 ))
22+ }
23+
24+ @Test
25+ fun testDay03PartTwo () {
26+ assertEquals(100450138 , day03.partTwo(DAY_03 ))
27+ }
28+
29+ companion object {
30+ private const val DAY_03 : String = " advent-of-code-input/2024/day03.input"
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments