diff --git a/build.gradle.kts b/build.gradle.kts index b5c3de5..98d96f2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -34,7 +34,7 @@ dependencies { implementation("io.micronaut.serde:micronaut-serde-jackson") implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}") - implementation("de.linkel.aoc:aoc-utils:1.0.0") + implementation("de.linkel.aoc:aoc-utils:1.0.1") runtimeOnly("ch.qos.logback:logback-classic") runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin") testImplementation("org.assertj:assertj-core:3.24.2") diff --git a/src/main/kotlin/de/linkel/aoc/Day03.kt b/src/main/kotlin/de/linkel/aoc/Day03.kt index 09b83f9..557abdc 100644 --- a/src/main/kotlin/de/linkel/aoc/Day03.kt +++ b/src/main/kotlin/de/linkel/aoc/Day03.kt @@ -2,6 +2,10 @@ package de.linkel.aoc import de.linkel.aoc.base.AbstractLinesAdventDay import de.linkel.aoc.base.QuizPart +import de.linkel.aoc.utils.append +import de.linkel.aoc.utils.extend +import de.linkel.aoc.utils.intersects +import de.linkel.aoc.utils.prepend import jakarta.inject.Singleton @Singleton @@ -9,6 +13,41 @@ class Day03: AbstractLinesAdventDay() { override val day = 3 override fun process(part: QuizPart, lines: Sequence): Int { - return 0 + val digitPattern = Regex("[0-9]+") + val symbolPattern = Regex("[^0-9.]") + val asteriskPattern = Regex("\\*") + val result = lines + .prepend("") + .append("") + .windowed(3) + .map { win -> + val allNumbersPerLine = win.map { digitPattern.findAll(it) }.map { it.toList() } + val allSymbols = win.flatMap { symbolPattern.findAll(it) }.toList() + val partNumbers = allNumbersPerLine[1] + .filter { match -> + val checkrange = match.range.extend(front = 1, back = 1) + allSymbols + .any { it.range.intersects(checkrange) } + } + .sumOf { it.value.toInt() } + val allNumbers = allNumbersPerLine.flatten() + val gears = asteriskPattern.findAll(win[1]) + .map { match -> + val checkrange = match.range.extend(front = 1, back = 1) + allNumbers + .filter { it.range.intersects(checkrange) } + .map { it.value.toInt() } + } + .filter { it.size == 2 } + .map { it[0] * it[1] } + .sum() + Pair(partNumbers, gears) + } + .fold(Pair(0,0)) { + sum, line -> + Pair(sum.first + line.first, sum.second + line.second) + } + return if (part == QuizPart.A) result.first + else result.second } } diff --git a/src/test/kotlin/de/linkel/aoc/Day03Test.kt b/src/test/kotlin/de/linkel/aoc/Day03Test.kt index 333efd8..d976714 100644 --- a/src/test/kotlin/de/linkel/aoc/Day03Test.kt +++ b/src/test/kotlin/de/linkel/aoc/Day03Test.kt @@ -2,12 +2,22 @@ package de.linkel.aoc class Day03Test: AbstractDayTest() { override val exampleA = """ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...${'$'}.*.... +.664.598.. """.trimIndent() - override val exampleSolutionA = 0 - override val solutionA = 0 + override val exampleSolutionA = 4361 + override val solutionA = 559667 - override val exampleSolutionB = 0 - override val solutionB = 0 + override val exampleSolutionB = 467835 + override val solutionB = 86841457 override val implementation = Day03() }