|
| 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 | +} |
0 commit comments