Add grid interface and generic implementation.

This commit is contained in:
Sven Weidauer 2024-12-11 20:14:42 +01:00
parent 013d3df0ae
commit 267ffda2e9
4 changed files with 46 additions and 22 deletions

View file

@ -1,6 +1,6 @@
class CharGrid(val rows: List<String>) {
val width = rows.firstOrNull()?.count() ?: 0
val height get() = rows.count()
class CharGrid(val rows: List<String>) : Grid<Char> {
override val width = rows.firstOrNull()?.count() ?: 0
override val height get() = rows.count()
init {
assert(rows.all { it.length == width })
@ -59,21 +59,20 @@ class CharGrid(val rows: List<String>) {
yieldAll(diagonals())
}
fun get(x: Int, y: Int): Char = rows[y][x]
override fun get(x: Int, y: Int): Char = rows[y][x]
fun find(char: Char): Coordinate? {
fun find(char: Char): Grid.Coordinate? {
for (y in rows.indices) {
val index = rows[y].indexOf(char)
if (index != -1) {
return Coordinate(index, y)
return Grid.Coordinate(index, y)
}
}
return null
}
fun inside(coordinate: Coordinate) = coordinate.x in 0..<width && coordinate.y in 0..<height
operator fun get(coordinate: Coordinate) = get(coordinate.x, coordinate.y)
data class Coordinate(val x: Int, val y: Int)
override fun <T> map(fn: (Char) -> T): Grid<T> {
val data = rows.flatMap { it.map(fn) }
return ListGrid(width, height, data)
}
}

View file

@ -0,0 +1,25 @@
interface Grid<T> {
val width: Int
val height: Int
fun <U> map(fn: (T) -> U): Grid<U>
fun get(x: Int, y: Int): T
data class Coordinate(val x: Int, val y: Int)
fun inside(coordinate: Coordinate) = coordinate.x in 0..<width && coordinate.y in 0..<height
}
operator fun <T> Grid<T>.get(coordinate: Grid.Coordinate) = get(coordinate.x, coordinate.y)
operator fun <T> Grid<T>.contains(coordinate: Grid.Coordinate) = inside(coordinate)
class ListGrid<T>(override val width: Int, override val height: Int, private val content: List<T>) : Grid<T> {
init {
assert(width * height == content.count())
}
override fun get(x: Int, y: Int): T = content[x + y * width]
override fun <U> map(fn: (T) -> U): Grid<U> = ListGrid(width, height, content.map(fn))
}

View file

@ -12,7 +12,7 @@ enum class Direction {
}
}
fun CharGrid.Coordinate.step(direction: Direction) =
fun Grid.Coordinate.step(direction: Direction) =
when (direction) {
Direction.North -> copy(y = y - 1)
Direction.East -> copy(x = x + 1)
@ -25,7 +25,7 @@ fun main() {
var position = grid.find('^') ?: error("Guard not found")
var direction = Direction.North
val visitedPositions = mutableSetOf<CharGrid.Coordinate>()
val visitedPositions = mutableSetOf<Grid.Coordinate>()
while (true) {
visitedPositions.add(position)

View file

@ -1,36 +1,36 @@
fun main() {
val grid = CharGrid.read("day8.txt")
val antennasByFrequency = mutableMapOf<Char, MutableList<CharGrid.Coordinate>>()
val antennasByFrequency = mutableMapOf<Char, MutableList<Grid.Coordinate>>()
for (y in 0..<grid.height) {
for (x in 0..<grid.width) {
val value = grid.get(x, y)
if (value != '.') {
antennasByFrequency.getOrPut(value) { mutableListOf() }.add(CharGrid.Coordinate(x, y))
antennasByFrequency.getOrPut(value) { mutableListOf() }.add(Grid.Coordinate(x, y))
}
}
}
fun isAntinode(coordinate: CharGrid.Coordinate, frequency: Char, antennas: List<CharGrid.Coordinate>) =
fun isAntinode(coordinate: Grid.Coordinate, frequency: Char, antennas: List<Grid.Coordinate>) =
antennas.any { antenna ->
val dx = antenna.x - coordinate.x
val dy = antenna.y - coordinate.y
val secondAntenna = CharGrid.Coordinate(antenna.x + dx, antenna.y + dy)
val secondAntenna = Grid.Coordinate(antenna.x + dx, antenna.y + dy)
secondAntenna != antenna && grid.inside(secondAntenna) && grid[secondAntenna] == frequency
}
fun isAntinode(coordinate: CharGrid.Coordinate) =
fun isAntinode(coordinate: Grid.Coordinate) =
antennasByFrequency.any { (frequency, positions) ->
isAntinode(coordinate, frequency, positions)
}
val antinodes = mutableSetOf<CharGrid.Coordinate>()
val antinodes = mutableSetOf<Grid.Coordinate>()
for (y in 0..<grid.height) {
for (x in 0..<grid.width) {
val coordinate = CharGrid.Coordinate(x, y)
val coordinate = Grid.Coordinate(x, y)
if (isAntinode(coordinate)) {
antinodes.add(coordinate)
}
@ -50,13 +50,13 @@ fun main() {
var pos = first
while (grid.inside(pos)) {
antinodes.add(pos)
pos = CharGrid.Coordinate(pos.x + dx, pos.y + dy)
pos = Grid.Coordinate(pos.x + dx, pos.y + dy)
}
pos = first
while (grid.inside(pos)) {
antinodes.add(pos)
pos = CharGrid.Coordinate(pos.x - dx, pos.y - dy)
pos = Grid.Coordinate(pos.x - dx, pos.y - dy)
}
}
}