Rename Grid to CharGrid

This commit is contained in:
Sven Weidauer 2024-12-11 20:03:25 +01:00
parent 1460448cff
commit 013d3df0ae
4 changed files with 17 additions and 17 deletions

View file

@ -1,4 +1,4 @@
class Grid(val rows: List<String>) {
class CharGrid(val rows: List<String>) {
val width = rows.firstOrNull()?.count() ?: 0
val height get() = rows.count()
@ -7,7 +7,7 @@ class Grid(val rows: List<String>) {
}
companion object {
fun read(name: String): Grid = Grid(readInput(name).toList())
fun read(name: String): CharGrid = CharGrid(readInput(name).toList())
}
fun chars(x: Int, y: Int, dx: Int, dy: Int) = Line(x, y, dx, dy)

View file

@ -1,5 +1,5 @@
fun main() {
val grid = Grid.read("day4.txt")
val grid = CharGrid.read("day4.txt")
val part1 = grid.all().fold(0) { acc, line ->
val string = line.joinToString(separator = "")
@ -18,7 +18,7 @@ fun main() {
}
fun Grid.xmasAt(x: Int, y: Int): Boolean {
fun CharGrid.xmasAt(x: Int, y: Int): Boolean {
assert(get(x, y) == 'A')
val tl = get(x - 1, y - 1)

View file

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

View file

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