-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new mixins for combinations/permutations, package mixins renamed to i…
…terables. still some tests missing
- Loading branch information
Showing
22 changed files
with
576 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.linkel.aoc.utils | ||
|
||
fun CharSequence.replaceIndex(idx: Int, char: Char): CharSequence { | ||
if (idx < 0 || idx >= length) | ||
throw IndexOutOfBoundsException("index $idx is out of bounds 0..${length-1}") | ||
val sb = StringBuilder() | ||
sb.appendRange(this, 0, idx) | ||
sb.append(char) | ||
sb.appendRange(this, idx + 1, length) | ||
return sb | ||
} | ||
|
||
fun String.replaceIndex(idx: Int, char: Char): String = | ||
(this as CharSequence).replaceIndex(idx, char).toString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package de.linkel.aoc.utils.grid | ||
|
||
import kotlin.math.abs | ||
import kotlin.math.sign | ||
|
||
data class Segment( | ||
val x: Int, | ||
val y: Int | ||
) { | ||
operator fun plus(v: Vector): Segment { | ||
return copy( | ||
x = x + v.deltaX, | ||
y = y + v.deltaY | ||
) | ||
} | ||
operator fun minus(p: Segment): Vector { | ||
return Vector( | ||
deltaX = x - p.x, | ||
deltaY = y - p.y | ||
) | ||
} | ||
|
||
operator fun rangeTo(p: Segment): List<Segment> { | ||
if (p == this) { | ||
return listOf(p) | ||
} | ||
val vector = p - this | ||
return if (vector.deltaY == 0) { | ||
(0 .. abs(vector.deltaX)) | ||
.map { i -> this.copy( | ||
x = x + (i) * vector.deltaX.sign | ||
)} | ||
} else if (vector.deltaX == 0) { | ||
(0 .. abs(vector.deltaY)) | ||
.map { i -> this.copy( | ||
y = y + (i) * vector.deltaY.sign | ||
)} | ||
} else { | ||
throw IllegalArgumentException("rangeTo only works in straight lines") | ||
} | ||
} | ||
|
||
override fun toString(): String { | ||
return "$x/$y" | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
lib/src/main/kotlin/de/linkel/aoc/utils/iterables/CombinationMixIn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package de.linkel.aoc.utils.iterables | ||
|
||
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])) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun <A, B> List<A>.combineWith(other: List<B>): Sequence<Pair<A, B>> { | ||
val list = this | ||
return sequence { | ||
list | ||
.forEach { a -> | ||
other.forEach { b -> | ||
yield(Pair(a, b)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun <T> List<List<T>>.combinations(): Sequence<List<T>> { | ||
val outer = this | ||
return sequence { | ||
val sizes = outer.map { it.size } | ||
val indices = outer.map { 0 }.toMutableList() | ||
while (indices.mapIndexed { i, o -> o < sizes[i] }.all { it }) { | ||
yield( | ||
indices.mapIndexed { i,o -> outer[i][o] } | ||
) | ||
if (outer.indices | ||
.firstOrNull { i -> | ||
indices[i] = indices[i] + 1 | ||
if (indices[i] >= sizes[i]) { | ||
indices[i] = 0 | ||
false | ||
} else true | ||
} == null) { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun <T> List<T>.permutations(maxSize: Int = -1): Sequence<List<T>> { | ||
val list = this | ||
return sequence { | ||
if (list.isEmpty() || maxSize == 0) { | ||
yield(emptyList()) | ||
} else { | ||
list.indices.forEach { i -> | ||
val item = list[i] | ||
list.withoutIndex(i).permutations(maxSize - 1) | ||
.forEach { | ||
yield(it + item) | ||
} | ||
} | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...linkel/aoc/utils/mixins/ConcatSequence.kt → ...kel/aoc/utils/iterables/ConcatSequence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
lib/src/main/kotlin/de/linkel/aoc/utils/iterables/FilterMixIn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package de.linkel.aoc.utils.iterables | ||
|
||
|
||
fun <T> List<T>.withoutIndex(blacklist: IntRange) = this.filterIndexed { idx, _ -> idx !in blacklist } | ||
fun <T> List<T>.withoutIndex(index: Int) = this.filterIndexed { idx, _ -> idx != index } |
2 changes: 1 addition & 1 deletion
2
.../linkel/aoc/utils/mixins/IntRangeMixIn.kt → ...nkel/aoc/utils/iterables/IntRangeMixIn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...linkel/aoc/utils/mixins/LongRangeMixIn.kt → ...kel/aoc/utils/iterables/LongRangeMixIn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...inkel/aoc/utils/mixins/NumberIterables.kt → ...el/aoc/utils/iterables/NumberIterables.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
lib/src/main/kotlin/de/linkel/aoc/utils/iterables/SplitSequenceMixIn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.linkel.aoc.utils.iterables | ||
|
||
|
||
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()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/src/main/kotlin/de/linkel/aoc/utils/rangeset/IntRangeSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/src/main/kotlin/de/linkel/aoc/utils/rangeset/LongRangeSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package de.linkel.aoc.utils | ||
|
||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
class StringTest { | ||
@Test | ||
fun `can replace s single character at a given position`() { | ||
Assertions.assertThat("test".replaceIndex(1, '3')).isEqualTo("t3st") | ||
} | ||
} |
Oops, something went wrong.