Skip to content

Commit 89c2be5

Browse files
committed
Grid: Implement a set operator for array access
1 parent 6fe2863 commit 89c2be5

File tree

2 files changed

+14
-1
lines changed
  • src
    • main/kotlin/de/ronny_h/aoc/extensions/grids
    • test/kotlin/de/ronny_h/aoc/extensions/grids

2 files changed

+14
-1
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/grids/Grid.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ abstract class Grid<T>(
7676
?: fallbackElement
7777
}
7878

79+
operator fun set(row: Int, col: Int, value: T) {
80+
grid[row][col] = value
81+
}
82+
7983
fun getAt(position: Coordinates) = get(position.row, position.col)
8084
fun setAt(position: Coordinates, element: T) {
81-
grid[position.row][position.col] = element
85+
this[position.row, position.col] = element
8286
}
8387

8488
/**

src/test/kotlin/de/ronny_h/aoc/extensions/grids/GridTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ class GridTest : StringSpec() {
8888
grid[1, 1] shouldBe '4'
8989
}
9090

91+
"set with array access sets the element at the given coordinates" {
92+
val grid = SimpleCharGrid(listOf("12", "34"))
93+
grid[1, 0] = '5'
94+
grid[0, 0] shouldBe '1'
95+
grid[0, 1] shouldBe '2'
96+
grid[1, 0] shouldBe '5'
97+
grid[1, 1] shouldBe '4'
98+
}
99+
91100
"subGridAt returns a sub grid at the given coordinates" {
92101
val input = """
93102
1234

0 commit comments

Comments
 (0)