Skip to content

Commit b8d2f75

Browse files
authored
Day 07 2024 (#266)
* Day 07 2024
1 parent 9c8e0b2 commit b8d2f75

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.peckb.aoc._2024.calendar.day07
2+
3+
import javax.inject.Inject
4+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
6+
class Day07 @Inject constructor(
7+
private val generatorFactory: InputGeneratorFactory,
8+
) {
9+
fun partOne(filename: String) = generatorFactory.forFile(filename).readAs(::calibration) { input ->
10+
input.filter { check(it, listOf(ADD, MULTIPLY)) }.sumOf { it.value }
11+
}
12+
13+
fun partTwo(filename: String) = generatorFactory.forFile(filename).readAs(::calibration) { input ->
14+
input.filter { check(it, listOf(ADD, MULTIPLY, CONCAT)) }.sumOf { it.value }
15+
}
16+
17+
fun check(calibration: Calibration, operands: List<Operand>): Boolean {
18+
val (n, m) = calibration.numbers.take(2)
19+
val otherNumbers = calibration.numbers.drop(2)
20+
return operands.any { op -> check(calibration.value, listOf(op.invoke(n, m)).plus(otherNumbers), operands) }
21+
}
22+
23+
fun check(targetVal: Long, remainingNumbers: List<Long>, operands: List<Operand>) : Boolean {
24+
if (remainingNumbers.isEmpty()) return false
25+
if (remainingNumbers.size == 1) return targetVal == remainingNumbers[0]
26+
if (remainingNumbers[0] > targetVal) return false
27+
28+
val (n, m) = remainingNumbers.take(2)
29+
val otherNumbers = remainingNumbers.drop(2)
30+
return operands.any { op -> check(targetVal, listOf(op.invoke(n, m)).plus(otherNumbers), operands) }
31+
}
32+
33+
private fun calibration(line: String): Calibration {
34+
return line.split(": ").let { (valueStr, numbersStr) ->
35+
val numbers = numbersStr.split(" ").map(String::toLong)
36+
Calibration(valueStr.toLong(), numbers)
37+
}
38+
}
39+
40+
companion object {
41+
val ADD = { n: Long, m: Long -> n + m }
42+
val MULTIPLY = { n: Long, m: Long -> n * m }
43+
val CONCAT = { n: Long, m: Long -> "$n$m".toLong() }
44+
}
45+
}
46+
47+
typealias Operand = (Long, Long) -> Long
48+
49+
data class Calibration(val value: Long, val numbers: List<Long>)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 7: Bridge Repair](https://adventofcode.com/2024/day/7)

src/test/kotlin/me/peckb/aoc/_2024/TestDayComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import me.peckb.aoc._2024.calendar.day03.Day03Test
66
import me.peckb.aoc._2024.calendar.day04.Day04Test
77
import me.peckb.aoc._2024.calendar.day05.Day05Test
88
import me.peckb.aoc._2024.calendar.day06.Day06Test
9+
import me.peckb.aoc._2024.calendar.day07.Day07Test
910
import javax.inject.Singleton
1011
import me.peckb.aoc.DayComponent
1112
import me.peckb.aoc.InputModule
@@ -20,4 +21,5 @@ internal interface TestDayComponent : DayComponent {
2021
fun inject(day04Test: Day04Test)
2122
fun inject(day05Test: Day05Test)
2223
fun inject(day06Test: Day06Test)
24+
fun inject(day07Test: Day07Test)
2325
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.peckb.aoc._2024.calendar.day07
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 Day07Test {
11+
@Inject
12+
lateinit var day07: Day07
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay07PartOne() {
21+
assertEquals(6231007345478, day07.partOne(DAY_07))
22+
}
23+
24+
@Test
25+
fun testDay07PartTwo() {
26+
assertEquals(333027885676693, day07.partTwo(DAY_07))
27+
}
28+
29+
companion object {
30+
private const val DAY_07: String = "advent-of-code-input/2024/day07.input"
31+
}
32+
}

0 commit comments

Comments
 (0)