diff --git a/2024/src/main/kotlin/CharGrid.kt b/2024/src/main/kotlin/CharGrid.kt index 9f014ee..3a4b0b9 100644 --- a/2024/src/main/kotlin/CharGrid.kt +++ b/2024/src/main/kotlin/CharGrid.kt @@ -1,6 +1,6 @@ -class CharGrid(val rows: List) { - val width = rows.firstOrNull()?.count() ?: 0 - val height get() = rows.count() +class CharGrid(val rows: List) : Grid { + 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) { 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.. map(fn: (Char) -> T): Grid { + val data = rows.flatMap { it.map(fn) } + return ListGrid(width, height, data) + } } diff --git a/2024/src/main/kotlin/Grid.kt b/2024/src/main/kotlin/Grid.kt new file mode 100644 index 0000000..fa72d3b --- /dev/null +++ b/2024/src/main/kotlin/Grid.kt @@ -0,0 +1,25 @@ +interface Grid { + val width: Int + val height: Int + + fun map(fn: (T) -> U): Grid + + fun get(x: Int, y: Int): T + + data class Coordinate(val x: Int, val y: Int) + + fun inside(coordinate: Coordinate) = coordinate.x in 0.. Grid.get(coordinate: Grid.Coordinate) = get(coordinate.x, coordinate.y) +operator fun Grid.contains(coordinate: Grid.Coordinate) = inside(coordinate) + +class ListGrid(override val width: Int, override val height: Int, private val content: List) : Grid { + init { + assert(width * height == content.count()) + } + + override fun get(x: Int, y: Int): T = content[x + y * width] + + override fun map(fn: (T) -> U): Grid = ListGrid(width, height, content.map(fn)) +} diff --git a/2024/src/main/kotlin/day6.kt b/2024/src/main/kotlin/day6.kt index 9e13ac9..6d10fae 100644 --- a/2024/src/main/kotlin/day6.kt +++ b/2024/src/main/kotlin/day6.kt @@ -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() + 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 b2edecf..3939826 100644 --- a/2024/src/main/kotlin/day8.kt +++ b/2024/src/main/kotlin/day8.kt @@ -1,36 +1,36 @@ fun main() { val grid = CharGrid.read("day8.txt") - val antennasByFrequency = mutableMapOf>() + val antennasByFrequency = mutableMapOf>() for (y in 0..) = + fun isAntinode(coordinate: Grid.Coordinate, frequency: Char, antennas: List) = 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() + val antinodes = mutableSetOf() for (y in 0..