|
| 1 | +// Advent of Code 2024 |
| 2 | +// By Sebastian Raaphorst, 2024. |
| 3 | + |
| 4 | +package common.gridalgorithms |
| 5 | + |
| 6 | +import common.intpos2d.* |
| 7 | + |
| 8 | +data class Region(val area: Int, val perimeter: Int, val edges: Int) |
| 9 | + |
| 10 | +fun findRegions(grid: Grid<Char>): List<Region> { |
| 11 | + val visited = mutableSetOf<IntPos2D>() |
| 12 | + val rows = grid.size |
| 13 | + val cols = grid[0].size |
| 14 | + |
| 15 | + fun neighbours(pos: IntPos2D): List<IntPos2D> = |
| 16 | + Direction.entries.map { dir -> |
| 17 | + pos + dir.delta |
| 18 | + }.filter { pos -> pos.first in 0 until rows && pos.second in 0 until cols } |
| 19 | + |
| 20 | + fun floodFill(start: IntPos2D): Region { |
| 21 | + val stack = mutableListOf(start) |
| 22 | + var area = 0 |
| 23 | + var perimeter = 0 |
| 24 | + val symbol = grid[start.first][start.second] |
| 25 | + var corners = 0 |
| 26 | + |
| 27 | + while (stack.isNotEmpty()) { |
| 28 | + val pos = stack.removeLast() |
| 29 | + if (pos !in visited) { |
| 30 | + visited.add(pos) |
| 31 | + area++ |
| 32 | + |
| 33 | + // Calculate perimeter: count neighbors that are not part of the same region, |
| 34 | + // as well as the walls that are out of bounds. |
| 35 | + val neighbours = neighbours(pos) |
| 36 | + val localPerimeter = neighbours.count { pos2 -> |
| 37 | + grid[pos2.first][pos2.second] != symbol |
| 38 | + } |
| 39 | + val outOfBounds = Direction.entries.map { dir -> pos + dir.delta } |
| 40 | + .count { pos2 -> pos2.first < 0 || pos2.first >= rows |
| 41 | + || pos2.second < 0 || pos2.second >= cols } |
| 42 | + perimeter += localPerimeter + outOfBounds |
| 43 | + |
| 44 | + // Calculate the corners, which will ultimately give us the number of |
| 45 | + // edges. Every corner is a shift in direction, indicating an edge. |
| 46 | + corners += Diagonals.count { (d1, d2) -> |
| 47 | + val side1 = grid[pos + d1.delta] |
| 48 | + val side2 = grid[pos + d2.delta] |
| 49 | + val corner = grid[pos + d1.delta + d2.delta] |
| 50 | + |
| 51 | + // Two cases: |
| 52 | + // 1. The symbol here is different from the corners: |
| 53 | + // ? B |
| 54 | + // B A |
| 55 | + // 2. The symbol is the same as the sides but different from the corner: |
| 56 | + // B A |
| 57 | + // A A |
| 58 | + (symbol != side1 && symbol != side2) || |
| 59 | + (symbol == side1 && symbol == side2 && symbol != corner) |
| 60 | + } |
| 61 | + |
| 62 | + // Add valid neighbors to the stack |
| 63 | + stack.addAll( |
| 64 | + neighbours(pos).filter { pos2 -> |
| 65 | + grid[pos2.first][pos2.second] == symbol && pos2 !in visited |
| 66 | + } |
| 67 | + ) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // area is A |
| 72 | + // perimeter is b |
| 73 | + return Region(area, perimeter, corners) |
| 74 | + } |
| 75 | + |
| 76 | + // Iterate over the grid and find all regions |
| 77 | + return (0 until rows).flatMap { x -> |
| 78 | + (0 until cols).mapNotNull { y -> |
| 79 | + if (IntPos2D(x, y) !in visited) floodFill(x to y) else null |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments