Skip to content

Commit ae4e28b

Browse files
committed
Add a toString function to Grid
The function returns a String representation of the Grid without printing it to System.out (like printGrid() does).
1 parent 4b2cbff commit ae4e28b

File tree

2 files changed

+41
-5
lines changed
  • src
    • main/kotlin/de/ronny_h/aoc/extensions
    • test/kotlin/de/ronny_h/aoc/extensions

2 files changed

+41
-5
lines changed

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package de.ronny_h.aoc.extensions
22

3+
import java.io.ByteArrayOutputStream
4+
import java.io.PrintStream
5+
import java.nio.charset.StandardCharsets
36
import kotlin.collections.MutableList
47

58
/**
@@ -74,7 +77,31 @@ abstract class Grid<T>(
7477
if (element == value) Coordinates(row, col) else null
7578
}.filterNotNull().first()
7679

80+
override fun toString(): String = toString(setOf())
81+
82+
fun toString(
83+
overrides: Set<Coordinates> = setOf(),
84+
overrideChar: Char = '#',
85+
): String {
86+
val out = ByteArrayOutputStream()
87+
PrintStream(out, true, StandardCharsets.UTF_8).use {
88+
printGrid(it, overrides = overrides, overrideChar = overrideChar)
89+
}
90+
return out.toString().trim()
91+
}
92+
93+
fun printGrid(
94+
overrides: Set<Coordinates> = setOf(),
95+
overrideChar: Char = '#',
96+
highlightPosition: Coordinates? = null,
97+
highlightDirection: Direction? = null,
98+
path: Map<Coordinates, Char> = mapOf(),
99+
) {
100+
printGrid(System.out, overrides, overrideChar, highlightPosition, highlightDirection, path)
101+
}
102+
77103
fun printGrid(
104+
writer: PrintStream,
78105
overrides: Set<Coordinates> = setOf(),
79106
overrideChar: Char = '#',
80107
highlightPosition: Coordinates? = null,
@@ -83,15 +110,15 @@ abstract class Grid<T>(
83110
) {
84111
forEachCoordinates { position, element ->
85112
if (highlightPosition == position && highlightDirection != null) {
86-
print(highlightDirection.asChar())
113+
writer.print(highlightDirection.asChar())
87114
} else if (path.contains(position)) {
88-
print(path[position])
115+
writer.print(path[position])
89116
} else if (overrides.contains(position)) {
90-
print(overrideChar)
117+
writer.print(overrideChar)
91118
} else {
92-
print(element)
119+
writer.print(element)
93120
}
94-
if (position.col == width - 1) println()
121+
if (position.col == width - 1) writer.println()
95122
}.last()
96123
}
97124
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,14 @@ class GridTest : StringSpec() {
194194
output shouldBe "1#${newLine}34$newLine"
195195
}
196196

197+
"toString returns a string representation without any overrides" {
198+
val grid = simpleCharGridOf(listOf("12", "34"))
199+
grid.toString() shouldBe "12${newLine}34"
200+
}
201+
202+
"toString with overrides returns a string representation with the given overrides" {
203+
val grid = simpleCharGridOf(listOf("12", "34"))
204+
grid.toString(setOf(Coordinates(1, 1)), 'o') shouldBe "12${newLine}3o"
205+
}
197206
}
198207
}

0 commit comments

Comments
 (0)