From 54e60855d012f0801a4f011c7901fc70aab3fa3b Mon Sep 17 00:00:00 2001 From: Stephan Linkel <251381+norganos@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:26:10 +0100 Subject: [PATCH] upgrade to lib 1.0.3 (new helper functions from the last days are now in lib) --- build.gradle.kts | 2 +- src/main/kotlin/de/linkel/aoc/Day03.kt | 8 +++--- src/main/kotlin/de/linkel/aoc/Day11.kt | 19 +------------ src/main/kotlin/de/linkel/aoc/Day13.kt | 39 +++----------------------- 4 files changed, 10 insertions(+), 58 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f5911d2..2d8f493 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.2") + implementation("de.linkel.aoc:aoc-utils:1.0.3") 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 30f8a0d..30c371a 100644 --- a/src/main/kotlin/de/linkel/aoc/Day03.kt +++ b/src/main/kotlin/de/linkel/aoc/Day03.kt @@ -2,10 +2,10 @@ package de.linkel.aoc import de.linkel.aoc.base.AbstractLinesAdventDay import de.linkel.aoc.base.QuizPart -import de.linkel.aoc.utils.mixins.append -import de.linkel.aoc.utils.mixins.extend -import de.linkel.aoc.utils.mixins.intersects -import de.linkel.aoc.utils.mixins.prepend +import de.linkel.aoc.utils.iterables.append +import de.linkel.aoc.utils.iterables.extend +import de.linkel.aoc.utils.iterables.intersects +import de.linkel.aoc.utils.iterables.prepend import jakarta.inject.Singleton @Singleton diff --git a/src/main/kotlin/de/linkel/aoc/Day11.kt b/src/main/kotlin/de/linkel/aoc/Day11.kt index d660134..ec46ec1 100644 --- a/src/main/kotlin/de/linkel/aoc/Day11.kt +++ b/src/main/kotlin/de/linkel/aoc/Day11.kt @@ -3,6 +3,7 @@ package de.linkel.aoc import de.linkel.aoc.base.AbstractLinesAdventDay import de.linkel.aoc.base.QuizPart import de.linkel.aoc.utils.grid.Point +import de.linkel.aoc.utils.iterables.combinationPairs import jakarta.inject.Singleton @Singleton @@ -49,21 +50,3 @@ class Day11: AbstractLinesAdventDay() { .sumOf { (it.first - it.second).manhattenDistance.toLong() } } } -fun List.combinationPairs( - withSelf: Boolean = false, - withMirrors: Boolean = false -): Sequence> { - val size = this.size - val list = this - return sequence { - (0 until size) - .forEach { i -> - ((if (withMirrors) 0 else i) until size) - .forEach { j -> - if (withSelf || i != j) { - yield(Pair(list[i], list[j])) - } - } - } - } -} diff --git a/src/main/kotlin/de/linkel/aoc/Day13.kt b/src/main/kotlin/de/linkel/aoc/Day13.kt index b4ef8ca..ac46910 100644 --- a/src/main/kotlin/de/linkel/aoc/Day13.kt +++ b/src/main/kotlin/de/linkel/aoc/Day13.kt @@ -2,6 +2,9 @@ package de.linkel.aoc import de.linkel.aoc.base.AbstractLinesAdventDay import de.linkel.aoc.base.QuizPart +import de.linkel.aoc.utils.iterables.combineWith +import de.linkel.aoc.utils.iterables.split +import de.linkel.aoc.utils.replaceIndex import jakarta.inject.Singleton import kotlin.math.min @@ -25,7 +28,7 @@ class Day13: AbstractLinesAdventDay() { } else { val mrows = rows.toMutableList() val mcols = cols.toMutableList() - coordinates(rows.size, cols.size) + rows.indices.toList().combineWith(cols.indices.toList()) .firstNotNullOf { pos -> val backup = mrows[pos.first][pos.second] val char = if (backup == '#') '.' else '#' @@ -58,37 +61,3 @@ class Day13: AbstractLinesAdventDay() { } ?: 0 } } - -fun String.replaceIndex(idx: Int, char: Char): String { - return "${this.substring(0, idx)}$char${this.substring(idx+1)}" -} - -fun coordinates(rows: Int, cols: Int): Sequence> { - return sequence { - (0 until rows) - .forEach { r -> - (0 until cols) - .forEach { c -> - yield(r to c) - } - } - } -} - -fun Sequence.split(predicate: (T) -> Boolean): Sequence> { - val input = this - return sequence { - val buffer = mutableListOf() - input.forEach { element -> - if (predicate(element)) { - yield(buffer.toList()) - buffer.clear() - } else { - buffer.add(element) - } - } - if (buffer.isNotEmpty()) { - yield(buffer.toList()) - } - } -}