Skip to content

Commit 99a8e55

Browse files
authored
Day 06 2025 (#300)
1 parent 8b40369 commit 99a8e55

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package me.peckb.aoc._2025.calendar.day06
2+
3+
import javax.inject.Inject
4+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
import kotlin.text.trim
6+
7+
class Day06 @Inject constructor(
8+
private val generatorFactory: InputGeneratorFactory,
9+
) {
10+
companion object {
11+
const val PRODUCT = '*'
12+
const val SUM = '+'
13+
}
14+
15+
fun partOne(filename: String) = generatorFactory.forFile(filename).read { input ->
16+
val numbersInEquations = mutableListOf<MutableList<Long>>()
17+
val operationsInEquations = mutableListOf<Char>()
18+
19+
val newList = { i : Int -> mutableListOf<Long>().also {
20+
numbersInEquations.add(i, it)
21+
}}
22+
23+
input.forEach { line ->
24+
line.trim().split("\\s+".toRegex()).forEachIndexed { index, dataElement ->
25+
val number = dataElement.toLongOrNull()
26+
if (number != null) {
27+
numbersInEquations.getOrElse(index) { i -> newList(i) }.add(number)
28+
} else {
29+
operationsInEquations.add(index, dataElement.first())
30+
}
31+
}
32+
}
33+
34+
numbersInEquations.zip(operationsInEquations).sumOf { (numbers, operation) ->
35+
when (operation) {
36+
SUM -> numbers.reduce(Long::plus)
37+
PRODUCT -> numbers.reduce(Long::times)
38+
else -> throw IllegalArgumentException("Illegal operation $operation")
39+
}
40+
}
41+
}
42+
43+
fun partTwo(filename: String) = generatorFactory.forFile(filename).read { input ->
44+
val numberLines = mutableListOf<String>()
45+
val operationData = mutableListOf<Pair<Char, Int>>()
46+
47+
var combinedTotal = 0L
48+
49+
// first we need to find out column spacing and operand data
50+
input.forEach { line ->
51+
val firstChar = line.first()
52+
if (firstChar == PRODUCT || firstChar == SUM) {
53+
var index = 0
54+
while(index < line.length) {
55+
val operation = line[index]
56+
if (index == line.length - 1) {
57+
// end row so our "length" should use the longest length line
58+
val length = (numberLines.maxOf { it.length } - line.length + 1)
59+
// and once we have our length, track the operation
60+
// since we're at the end of our input string we don't have a buffer space
61+
operationData.add(operation to length)
62+
index++
63+
} else {
64+
// not the end row - go char by char until we find the next operand
65+
var length = 0
66+
do { val next = line[index + ++length] } while (next == ' ')
67+
// and once we have our length, track the operation
68+
// there is always a buffer space between equations so offset by that
69+
operationData.add(operation to length - 1)
70+
index += length
71+
}
72+
}
73+
} else {
74+
numberLines.add(line)
75+
}
76+
77+
// now that we have our operand and column length we can do math!
78+
var bufferIndex = 0
79+
operationData.forEach { (operation, dataSize) ->
80+
var total = if (operation == PRODUCT) 1L else 0L
81+
var digitIndex = 0
82+
83+
while (digitIndex < dataSize) {
84+
val digit = numberLines.map { line ->
85+
// grab the number to use, the blank space in the string, ...
86+
// or the string wasn't that long and pretend we have trailing spaces
87+
line.getOrElse(bufferIndex + dataSize - digitIndex - 1) { ' ' }
88+
}.joinToString(separator = "").trim().toLong()
89+
90+
if (operation == PRODUCT) total *= digit else total += digit
91+
92+
digitIndex++
93+
}
94+
95+
// don't forget the buffer space when incrementing
96+
bufferIndex += dataSize + 1
97+
// and track the total math sum
98+
combinedTotal += total
99+
}
100+
}
101+
102+
combinedTotal
103+
}
104+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 6: Trash Compactor](https://adventofcode.com/2025/day/6)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import me.peckb.aoc._2025.calendar.day02.Day02Test
55
import me.peckb.aoc._2025.calendar.day03.Day03Test
66
import me.peckb.aoc._2025.calendar.day04.Day04Test
77
import me.peckb.aoc._2025.calendar.day05.Day05Test
8+
import me.peckb.aoc._2025.calendar.day06.Day06Test
89
import javax.inject.Singleton
910
import me.peckb.aoc.DayComponent
1011
import me.peckb.aoc.InputModule
@@ -18,4 +19,5 @@ internal interface TestDayComponent : DayComponent {
1819
fun inject(day03Test: Day03Test)
1920
fun inject(day04Test: Day04Test)
2021
fun inject(day05Test: Day05Test)
22+
fun inject(day06Test: Day06Test)
2123
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.peckb.aoc._2025.calendar.day06
2+
3+
import javax.inject.Inject
4+
5+
import me.peckb.aoc._2025.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 Day06Test {
11+
@Inject
12+
lateinit var day06: Day06
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay06PartOne() {
21+
assertEquals(4583860641327, day06.partOne(DAY_06))
22+
}
23+
24+
@Test
25+
fun testDay06PartTwo() {
26+
assertEquals(11602774058280, day06.partTwo(DAY_06))
27+
}
28+
29+
companion object {
30+
private const val DAY_06: String = "advent-of-code-input/2025/day06.input"
31+
}
32+
}

0 commit comments

Comments
 (0)