Skip to content

Commit 19f8199

Browse files
committed
Prepare for 2018-17 (Reservoir Research)
* implement a map-backed char grid * parse the input to the grid
1 parent 91b3738 commit 19f8199

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package de.ronny_h.aoc.year2018.day17
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.grids.Coordinates
5+
import java.io.ByteArrayOutputStream
6+
import java.io.PrintStream
7+
import kotlin.math.max
8+
import kotlin.math.min
9+
import kotlin.text.Charsets.UTF_8
10+
11+
fun main() = ReservoirResearch().run(0, 0)
12+
13+
class ReservoirResearch : AdventOfCode<Int>(2018, 17) {
14+
override fun part1(input: List<String>): Int {
15+
return 0
16+
}
17+
18+
override fun part2(input: List<String>): Int {
19+
return 0
20+
}
21+
}
22+
23+
class VerticalSliceOfGround(input: List<String>) {
24+
private val clay = '#'
25+
private val sand = '.'
26+
private val waterSpring = '+'
27+
28+
private val slice = MapBackedCharGrid(Coordinates(500, 0), sand)
29+
30+
init {
31+
slice[Coordinates(500, 0)] = waterSpring
32+
input.forEach {
33+
val (single, range) = it.split(", ")
34+
if (single.startsWith('x')) {
35+
slice[single.intAfter("x="), range.intRangeAfter("y=")] = clay
36+
} else {
37+
slice[range.intRangeAfter("x="), single.intAfter("y=")] = clay
38+
}
39+
}
40+
}
41+
42+
override fun toString(): String = slice.toString()
43+
44+
private fun String.intAfter(delimiter: String): Int = substringAfter(delimiter).toInt()
45+
46+
private fun String.intRangeAfter(delimiter: String): IntRange {
47+
val (from, to) = substringAfter(delimiter).split("..").map(String::toInt)
48+
return from..to
49+
}
50+
}
51+
52+
class MapBackedCharGrid(center: Coordinates, default: Char) {
53+
private var minX = center.x
54+
private var maxX = center.x
55+
private var minY = center.y
56+
private var maxY = center.y
57+
58+
private val slice = mutableMapOf<Coordinates, Char>().withDefault { default }
59+
60+
operator fun set(position: Coordinates, value: Char) {
61+
minX = min(minX, position.x)
62+
minY = min(minY, position.y)
63+
maxX = max(maxX, position.x)
64+
maxY = max(maxY, position.y)
65+
slice[position] = value
66+
}
67+
68+
operator fun set(x: Int, y: Int, value: Char) = set(Coordinates(x, y), value)
69+
70+
operator fun get(position: Coordinates) = slice.getValue(position)
71+
72+
operator fun set(x: Int, yRange: IntRange, value: Char) {
73+
for (y in yRange) {
74+
this[Coordinates(x, y)] = value
75+
}
76+
}
77+
78+
operator fun set(xRange: IntRange, y: Int, value: Char) {
79+
for (x in xRange) {
80+
this[Coordinates(x, y)] = value
81+
}
82+
}
83+
84+
override fun toString(): String {
85+
val out = ByteArrayOutputStream()
86+
PrintStream(out, true, UTF_8).use {
87+
for (y in minY..maxY) {
88+
for (x in minX - 1..maxX + 1) {
89+
it.print(this[Coordinates(x, y)])
90+
}
91+
it.print('\n')
92+
}
93+
}
94+
return out.toString(UTF_8).trim()
95+
}
96+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package de.ronny_h.aoc.year2018.day17
2+
3+
import de.ronny_h.aoc.extensions.asList
4+
import io.kotest.core.spec.style.StringSpec
5+
import io.kotest.matchers.shouldBe
6+
7+
class ReservoirResearchTest : StringSpec({
8+
9+
val input = """
10+
x=495, y=2..7
11+
y=7, x=495..501
12+
x=501, y=3..7
13+
x=498, y=2..4
14+
x=506, y=1..2
15+
x=498, y=10..13
16+
x=504, y=10..13
17+
y=13, x=498..504
18+
""".asList()
19+
20+
"input can be parsed" {
21+
val expectedSlice = """
22+
......+.......
23+
............#.
24+
.#..#.......#.
25+
.#..#..#......
26+
.#..#..#......
27+
.#.....#......
28+
.#.....#......
29+
.#######......
30+
..............
31+
..............
32+
....#.....#...
33+
....#.....#...
34+
....#.....#...
35+
....#######...
36+
""".trimIndent()
37+
VerticalSliceOfGround(input).toString() shouldBe expectedSlice
38+
}
39+
40+
"part 1" {
41+
val input = listOf("")
42+
ReservoirResearch().part1(input) shouldBe 0
43+
}
44+
45+
"part 2" {
46+
val input = listOf("")
47+
ReservoirResearch().part2(input) shouldBe 0
48+
}
49+
})

0 commit comments

Comments
 (0)