From 013d3df0aed78e5071067f212c208ebca4659f7b Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 11 Dec 2024 20:03:25 +0100 Subject: [PATCH] Rename Grid to CharGrid --- 2024/src/main/kotlin/{Grid.kt => CharGrid.kt} | 4 ++-- 2024/src/main/kotlin/day4.kt | 4 ++-- 2024/src/main/kotlin/day6.kt | 6 +++--- 2024/src/main/kotlin/day8.kt | 20 +++++++++---------- 4 files changed, 17 insertions(+), 17 deletions(-) rename 2024/src/main/kotlin/{Grid.kt => CharGrid.kt} (94%) diff --git a/2024/src/main/kotlin/Grid.kt b/2024/src/main/kotlin/CharGrid.kt similarity index 94% rename from 2024/src/main/kotlin/Grid.kt rename to 2024/src/main/kotlin/CharGrid.kt index dac44f2..9f014ee 100644 --- a/2024/src/main/kotlin/Grid.kt +++ b/2024/src/main/kotlin/CharGrid.kt @@ -1,4 +1,4 @@ -class Grid(val rows: List) { +class CharGrid(val rows: List) { val width = rows.firstOrNull()?.count() ?: 0 val height get() = rows.count() @@ -7,7 +7,7 @@ class Grid(val rows: List) { } 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) diff --git a/2024/src/main/kotlin/day4.kt b/2024/src/main/kotlin/day4.kt index 19aa5b9..018120a 100644 --- a/2024/src/main/kotlin/day4.kt +++ b/2024/src/main/kotlin/day4.kt @@ -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) diff --git a/2024/src/main/kotlin/day6.kt b/2024/src/main/kotlin/day6.kt index 3aa4ee1..9e13ac9 100644 --- a/2024/src/main/kotlin/day6.kt +++ b/2024/src/main/kotlin/day6.kt @@ -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() + val visitedPositions = mutableSetOf() while (true) { visitedPositions.add(position) diff --git a/2024/src/main/kotlin/day8.kt b/2024/src/main/kotlin/day8.kt index 97db043..b2edecf 100644 --- a/2024/src/main/kotlin/day8.kt +++ b/2024/src/main/kotlin/day8.kt @@ -1,36 +1,36 @@ fun main() { - val grid = Grid.read("day8.txt") + val grid = CharGrid.read("day8.txt") - val antennasByFrequency = mutableMapOf>() + val antennasByFrequency = mutableMapOf>() for (y in 0..) = + fun isAntinode(coordinate: CharGrid.Coordinate, frequency: Char, antennas: List) = 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() + val antinodes = mutableSetOf() for (y in 0..