Compare commits

...

2 Commits

Author SHA1 Message Date
2b3549c84c Day 6. 2025-12-06 06:46:07 +01:00
128ffbb10f English... 2025-12-06 06:46:02 +01:00
2 changed files with 57 additions and 1 deletions

56
src/aoc2025/Day06.kt Normal file
View File

@ -0,0 +1,56 @@
package aoc2025
import println
import readInput
import splitLongs
/*
--- Day 6: Trash Compactor ---
https://adventofcode.com/2025/day/6
*/
fun main() {
val inlineTestInput = """
123 328 51 64
45 64 387 23
6 98 215 314
* + * +
"""
fun part1(input: List<String>): Long {
val numbers = input.dropLast(1).map { it.splitLongs().toLongArray() }.toList()
val ops = input.last().split(" ").filter(String::isNotBlank)
return ops.withIndex().sumOf { (c, op) -> if (op == "+") numbers.sumOf { it[c] } else numbers.fold(1L) { acc, row -> acc * row[c] } }
}
fun part2(input: List<String>): Long {
val numbers = ArrayList<LongArray>()
val maxLineLength = input.maxOf { it.length }
val spaceMask = BooleanArray(maxLineLength)
input.forEach { it.withIndex().filter { (_, ch) -> ch != ' ' }.forEach { (c, _) -> spaceMask[c] = true } }
val numColl = ArrayList<Long>()
for (c in 0 until maxLineLength) {
if (spaceMask[c]) numColl.add(input.dropLast(1).map { it[c] }.joinToString("").trim().toLong())
if (!spaceMask[c] || c == maxLineLength - 1) {
numbers.add(numColl.toLongArray())
numColl.clear()
}
}
return input.last().split(" ").filter(String::isNotBlank).withIndex()
.sumOf { (c, op) -> if (op == "+") numbers[c].sum() else numbers[c].fold(1L) { acc, row -> acc * row } }
}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("aoc2025/Day06_test")
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 4277556L)
check(testInputPart2Result == 3263827L)
val input = readInput("aoc2025/Day06")
part1(input).println()
part2(input).println()
}

View File

@ -69,7 +69,7 @@ class Downloader(val year: Int, val packageName: String, val sessionCookie: Stri
break break
} }
while (day == lastDay && LocalTime.now(ZoneId.of("UTC+1")).hour == 5) { while (day == lastDay && LocalTime.now(ZoneId.of("UTC+1")).hour == 5) {
println("Waiting until puzzle is available...") println("Waiting for puzzle to become available...")
runBlocking { runBlocking {
if (LocalTime.now(ZoneId.of("UTC+1")).minute < 59) { if (LocalTime.now(ZoneId.of("UTC+1")).minute < 59) {
delay(1.minutes) delay(1.minutes)