Skip to content

Commit e9bbe3b

Browse files
committed
Add convenience function to get column from a grid
1 parent c2d011b commit e9bbe3b

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This can be added to a [Kotlin Advent of Code template repository](https://githu
1010

1111
```kts
1212
dependencies {
13-
implementation("com.github.jsoberg:Kotlin-AoC-Utilities:2025.2")
13+
implementation("com.github.jsoberg:Kotlin-AoC-Utilities:2025.3")
1414
}
1515
```
1616

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Publishing {
22
const val ArtifactId = "Kotlin-AoC-Utilities"
33
const val GroupId = "com.github.jsoberg"
4-
const val Version = "2025.2"
4+
const val Version = "2025.3"
55
}

src/main/kotlin/com/soberg/aoc/utlities/datastructures/Grid2D.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ data class Grid2D<T>(
2929

3030
infix operator fun contains(location: Location): Boolean = isInBounds(location)
3131

32+
/** @return The column element values for column at index [col], or an error if [col] is not a valid index for this grid. */
33+
fun column(col: Int): List<T> {
34+
if (!isInBounds(row = 0, col = col)) {
35+
error("Column at index $col does not exist")
36+
}
37+
38+
return (0 until rowSize).map { row ->
39+
get(row = row, col = col)
40+
}
41+
}
42+
3243
/** @return true if the specified [location] is in the bounds of this grid, false if not. */
3344
fun isInBounds(location: Location): Boolean = isInBounds(location.row, location.col)
3445

src/test/kotlin/com/soberg/aoc/utlities/datastructures/Grid2DTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ class Grid2DTest {
5050
.isEqualTo(4444)
5151
}
5252

53+
@Test
54+
fun `return expected elements for column`() {
55+
val actual = testGrid.column(2)
56+
assertThat(actual)
57+
.containsExactly(3, 33, 333, 3333, 33333)
58+
}
59+
60+
@Test
61+
fun `throw error when column doesn't exist`() {
62+
assertThrows<IllegalStateException> {
63+
testGrid.column(100)
64+
}
65+
}
66+
5367
@Test
5468
fun `return expected element for elementAt`() {
5569
val actual = testGrid.elementAt(Location(2, 2), Direction.NorthWest, 2)

0 commit comments

Comments
 (0)