Skip to content

Commit ff25fb1

Browse files
committed
Pull the find() function up to the abstract Grid
1 parent 21a4e65 commit ff25fb1

File tree

5 files changed

+42
-18
lines changed

5 files changed

+42
-18
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ abstract class Grid<T>(
6565
}
6666
}
6767

68+
/**
69+
* Returns the first element matching the given `value`.
70+
*
71+
* @throws NoSuchElementException If no matching element can be found.
72+
*/
73+
fun find(value: T): Coordinates = forEachElement { row, col, element ->
74+
if (element == value) Coordinates(row, col) else null
75+
}.filterNotNull().first()
76+
6877
fun printGrid(
6978
overrides: Set<Coordinates> = setOf(),
7079
overrideChar: Char = '#',

src/main/kotlin/de/ronny_h/aoc/year24/day06/GuardGallivant.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ private class Lab(input: List<String>) : Grid<Char>(input, ' ') {
5858
private val offMap = nullElement
5959
override fun Char.toElementType() = this
6060

61-
private fun findTheGuard(): Coordinates = forEachElement { row, col, element ->
62-
if (element == guard) Coordinates(row, col) else null
63-
}.filterNotNull().first()
64-
6561
@OptIn(ExperimentalCoroutinesApi::class)
6662
fun freePositions(scope: CoroutineScope, bufferSize: Int) = scope.produce(capacity = bufferSize) {
6763
forEachElement { row, col, char ->
@@ -78,7 +74,7 @@ private class Lab(input: List<String>) : Grid<Char>(input, ' ') {
7874
visit: (Coordinates, Direction) -> Boolean,
7975
) {
8076
var direction = NORTH
81-
var position = findTheGuard()
77+
var position = find(guard)
8278
var doWalkFurther = true
8379

8480
while (getAt(position) != offMap && doWalkFurther) {

src/main/kotlin/de/ronny_h/aoc/year24/day15/WarehouseWoes.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ abstract class Warehouse(input: List<String>) : Grid<Char>(input, '#') {
5656
protected abstract fun tryToMove(from: Coordinates, direction: Direction, thing: Char): Boolean
5757

5858
fun moveRobot(movements: List<Direction>) {
59-
movements.fold(findTheRobot()) { position, direction ->
59+
movements.fold(find(robot)) { position, direction ->
6060
if (tryToMove(position, direction, robot)) {
6161
position + direction
6262
} else {
@@ -65,10 +65,6 @@ abstract class Warehouse(input: List<String>) : Grid<Char>(input, '#') {
6565
}
6666
}
6767

68-
private fun findTheRobot(): Coordinates = forEachElement { row, col, element ->
69-
if (element == robot) Coordinates(row, col) else null
70-
}.filterNotNull().first()
71-
7268
// GPS = Goods Positioning System
7369
fun sumGPSCoordinates(): Int = forEachElement { row, col, element ->
7470
if (element == leftGoodsChar) {

src/main/kotlin/de/ronny_h/aoc/year24/day20/RaceCondition.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ private class RaceTrack(input: List<String>) : Grid<Char>(input, '#') {
2727
private val start = find('S')
2828
private val goal = find('E')
2929

30-
private fun find(symbol: Char): Coordinates = forEachElement { row, col, elem ->
31-
when (elem) {
32-
symbol -> Coordinates(row, col)
33-
else -> null
34-
}
35-
}.filterNotNull().first()
36-
3730
fun shortestPath(): ShortestPath<Coordinates> {
3831
val neighbours: (Coordinates) -> List<Coordinates> = { node ->
3932
Direction

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package de.ronny_h.aoc.extensions
22

33
import com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut
4+
import io.kotest.assertions.throwables.shouldThrow
45
import io.kotest.core.spec.style.StringSpec
6+
import io.kotest.data.forAll
7+
import io.kotest.data.row
58
import io.kotest.matchers.shouldBe
69
import io.kotest.matchers.shouldNotBe
710

@@ -14,12 +17,21 @@ class GridTest : StringSpec() {
1417
private val newLine: String = System.lineSeparator()
1518

1619
init {
17-
1820
"a grid can be constructed from List of String" {
1921
val grid = simpleCharGridOf(listOf("12", "34"))
2022
grid shouldNotBe null
2123
}
2224

25+
"a grid can be constructed with overrides" {
26+
val grid = object : Grid<Char>(2, 2, ' ', 'x', listOf(Coordinates(1, 1))) {
27+
override fun Char.toElementType() = this
28+
}
29+
grid[0, 0] shouldBe ' '
30+
grid[0, 1] shouldBe ' '
31+
grid[1, 0] shouldBe ' '
32+
grid[1, 1] shouldBe 'x'
33+
}
34+
2335
"width and height have the right values" {
2436
val grid = simpleCharGridOf(listOf("00", "00", "00"))
2537
grid.height shouldBe 3
@@ -101,6 +113,24 @@ class GridTest : StringSpec() {
101113
)
102114
}
103115

116+
"find() finds all Coordinates" {
117+
val grid = simpleCharGridOf(listOf("12", "34"))
118+
forAll(
119+
row('1', Coordinates(0, 0)),
120+
row('2', Coordinates(0, 1)),
121+
row('3', Coordinates(1, 0)),
122+
row('4', Coordinates(1, 1)),
123+
) { char, result ->
124+
grid.find(char) shouldBe result
125+
}
126+
}
127+
128+
"find() throws a NoSuchElementException if the value cannot be found" {
129+
shouldThrow<NoSuchElementException> {
130+
simpleCharGridOf(listOf("12", "34")).find('5')
131+
}
132+
}
133+
104134
"printGrid prints the grid" {
105135
val output = tapSystemOut {
106136
val grid = simpleCharGridOf(listOf("12", "34"))

0 commit comments

Comments
 (0)