Skip to content

Commit

Permalink
upgrade to lib 1.0.3 (new helper functions from the last days are now…
Browse files Browse the repository at this point in the history
… in lib)
  • Loading branch information
norganos committed Dec 13, 2023
1 parent 113b355 commit 54e6085
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 58 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/de/linkel/aoc/Day03.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 1 addition & 18 deletions src/main/kotlin/de/linkel/aoc/Day11.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,21 +50,3 @@ class Day11: AbstractLinesAdventDay<Long>() {
.sumOf { (it.first - it.second).manhattenDistance.toLong() }
}
}
fun <T> List<T>.combinationPairs(
withSelf: Boolean = false,
withMirrors: Boolean = false
): Sequence<Pair<T,T>> {
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]))
}
}
}
}
}
39 changes: 4 additions & 35 deletions src/main/kotlin/de/linkel/aoc/Day13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,7 +28,7 @@ class Day13: AbstractLinesAdventDay<Int>() {
} 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 '#'
Expand Down Expand Up @@ -58,37 +61,3 @@ class Day13: AbstractLinesAdventDay<Int>() {
} ?: 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<Pair<Int, Int>> {
return sequence {
(0 until rows)
.forEach { r ->
(0 until cols)
.forEach { c ->
yield(r to c)
}
}
}
}

fun <T> Sequence<T>.split(predicate: (T) -> Boolean): Sequence<List<T>> {
val input = this
return sequence {
val buffer = mutableListOf<T>()
input.forEach { element ->
if (predicate(element)) {
yield(buffer.toList())
buffer.clear()
} else {
buffer.add(element)
}
}
if (buffer.isNotEmpty()) {
yield(buffer.toList())
}
}
}

0 comments on commit 54e6085

Please sign in to comment.