Skip to content

Commit c04beb1

Browse files
committed
Reuse transpose function in Grid2d
1 parent 055cfb9 commit c04beb1

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

src/main/kotlin/adventofcode/common/Collections.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ inline fun <reified T : Any?> List<List<T>>.neighbors(
6666
* D E F H E B
6767
* G H I I F C
6868
*/
69-
inline fun <reified T : Any?> Collection<Collection<T>>.transpose(): Collection<Collection<T>> {
69+
fun <T> Collection<Collection<T>>.transpose(): List<List<T>> {
7070
val result = first().map { mutableListOf<T>() }
7171
forEach { list -> result.zip(list).forEach { it.first.add(it.second) } }
72-
return result.map { it.reversed() }
72+
return result.map(List<T>::reversed)
7373
}
7474

7575
/**

src/main/kotlin/adventofcode/common/spatial/Grid2d.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package adventofcode.common.spatial
22

3+
import adventofcode.common.transpose
4+
35
data class Grid2d<T>(
46
val values: List<List<T>>,
57
) {
@@ -84,11 +86,7 @@ data class Grid2d<T>(
8486
* [D, E, F] [H, E, B]
8587
* [G, H, I] [I, F, C]
8688
*/
87-
fun rotateClockwise(): Grid2d<T> {
88-
val result = values.first().map { mutableListOf<T>() }
89-
values.forEach { list -> result.zip(list).forEach { it.first.add(it.second) } }
90-
return Grid2d(result.map(List<T>::reversed))
91-
}
89+
fun rotateClockwise(): Grid2d<T> = Grid2d(values.transpose())
9290

9391
/**
9492
* Returns a new grid by rotating this grid 90deg clockwise.

0 commit comments

Comments
 (0)