|
| 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 | +} |
0 commit comments