Skip to content

Commit

Permalink
Day 03 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
peckb1 committed Dec 3, 2024
1 parent 67f8006 commit 05ba3dc
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ as the value which passed the given day/phase combination
* Day 20
* 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

#### 2024
* Day 03
* Regex shining today! Sorting by the match location made the single list of all matches easy to scan through.

### Interesting approaches:

#### 2015
Expand Down
45 changes: 45 additions & 0 deletions src/main/kotlin/me/peckb/aoc/_2024/calendar/day03/Day03.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.peckb.aoc._2024.calendar.day03

import javax.inject.Inject
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory

class Day03 @Inject constructor(
private val generatorFactory: InputGeneratorFactory,
) {
fun partOne(filename: String) = generatorFactory.forFile(filename).read { lines ->
lines.sumOf { line ->
val matches = "mul\\(\\d+,\\d+\\)".toRegex().findAll(line)

matches.sumOf { match -> match.value.getMultiplicationResult() }
}
}

fun partTwo(filename: String) = generatorFactory.forFile(filename).read { lines ->
var enabled = true

lines.sumOf { line ->
val mulMatches = "mul\\(\\d+,\\d+\\)".toRegex().findAll(line)
val doMatches = "do\\(\\)".toRegex().findAll(line)
val dontMatches = "don\'t\\(\\)".toRegex().findAll(line)

val sortedMatches = (mulMatches + doMatches + dontMatches).sortedBy { it.range.first }

var count = 0L

sortedMatches.forEach { match ->
val value = match.value

when (value.take(3)) {
"do(" -> enabled = true
"don" -> enabled = false
"mul" -> if (enabled) { count += value.getMultiplicationResult() }
}
}

count
}
}

private fun String.getMultiplicationResult() =
this.drop(4).dropLast(1).split(",").map { it.toInt() }.reduce(Int::times)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## [Day 3: Mull It Over](https://adventofcode.com/2024/day/3)
2 changes: 2 additions & 0 deletions src/test/kotlin/me/peckb/aoc/_2024/TestDayComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.peckb.aoc._2024

import dagger.Component
import me.peckb.aoc._2024.calendar.day02.Day02Test
import me.peckb.aoc._2024.calendar.day03.Day03Test
import javax.inject.Singleton
import me.peckb.aoc.DayComponent
import me.peckb.aoc.InputModule
Expand All @@ -12,4 +13,5 @@ import me.peckb.aoc._2024.calendar.day01.Day01Test
internal interface TestDayComponent : DayComponent {
fun inject(day01Test: Day01Test)
fun inject(day02Test: Day02Test)
fun inject(day03Test: Day03Test)
}
32 changes: 32 additions & 0 deletions src/test/kotlin/me/peckb/aoc/_2024/calendar/day03/Day03Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.peckb.aoc._2024.calendar.day03

import javax.inject.Inject

import me.peckb.aoc._2024.DaggerTestDayComponent
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

internal class Day03Test {
@Inject
lateinit var day03: Day03

@BeforeEach
fun setup() {
DaggerTestDayComponent.create().inject(this)
}

@Test
fun testDay03PartOne() {
assertEquals(173517243, day03.partOne(DAY_03))
}

@Test
fun testDay03PartTwo() {
assertEquals(100450138, day03.partTwo(DAY_03))
}

companion object {
private const val DAY_03: String = "advent-of-code-input/2024/day03.input"
}
}

0 comments on commit 05ba3dc

Please sign in to comment.