11package de.ronny_h.aoc.extensions
22
3+ import java.io.ByteArrayOutputStream
4+ import java.io.PrintStream
5+ import java.nio.charset.StandardCharsets
36import 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}
0 commit comments