|
| 1 | +module main |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +struct Pos { |
| 6 | + row int |
| 7 | + col int |
| 8 | +} |
| 9 | + |
| 10 | +struct Offset { |
| 11 | + dr int |
| 12 | + dc int |
| 13 | +} |
| 14 | + |
| 15 | +fn count_adjacent(grid []u8, rows int, cols int, i int, j int, neighbors []Offset) int { |
| 16 | + mut count := 0 |
| 17 | + |
| 18 | + for offset in neighbors { |
| 19 | + ni := i + offset.dr |
| 20 | + nj := j + offset.dc |
| 21 | + |
| 22 | + if ni >= 0 && nj >= 0 && ni < rows && nj < cols { // inside grid |
| 23 | + nidx := ni * cols + nj |
| 24 | + if grid[nidx] == `@` { |
| 25 | + count++ |
| 26 | + if count >= 4 { |
| 27 | + break |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return count |
| 33 | +} |
| 34 | + |
| 35 | +fn main() { |
| 36 | + // Read input file |
| 37 | + content := os.read_file('department.input') or { panic('Failed to read file: ${err}') } |
| 38 | + lines := content.split_into_lines() |
| 39 | + |
| 40 | + mut grid_lines := []string{} |
| 41 | + for line in lines { |
| 42 | + if line.len > 0 { |
| 43 | + grid_lines << line |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + rows := grid_lines.len |
| 48 | + cols := grid_lines[0].len |
| 49 | + |
| 50 | + mut grid1 := []u8{len: rows * cols, cap: rows * cols} |
| 51 | + mut grid2 := []u8{len: rows * cols, cap: rows * cols} |
| 52 | + |
| 53 | + for i in 0 .. rows { |
| 54 | + line := grid_lines[i] |
| 55 | + row_start := i * cols |
| 56 | + for j in 0 .. cols { |
| 57 | + grid1[row_start + j] = line[j] |
| 58 | + grid2[row_start + j] = line[j] |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + neighbors := [ |
| 63 | + Offset{-1, -1}, // diagonal above left |
| 64 | + Offset{-1, 0}, // above |
| 65 | + Offset{-1, 1}, // diagional above right |
| 66 | + Offset{0, -1}, // left |
| 67 | + Offset{0, 1}, // right |
| 68 | + Offset{1, -1}, // diagonal below left |
| 69 | + Offset{1, 0}, // below |
| 70 | + Offset{1, 1}, // diagonal below right |
| 71 | + ] |
| 72 | + |
| 73 | + // Part 1 |
| 74 | + mut part1_count := 0 |
| 75 | + for i in 0 .. rows { |
| 76 | + row_start := i * cols |
| 77 | + for j in 0 .. cols { |
| 78 | + idx := row_start + j |
| 79 | + if grid1[idx] == `@` { |
| 80 | + adj_count := count_adjacent(grid1, rows, cols, i, j, neighbors) |
| 81 | + if adj_count < 4 { |
| 82 | + part1_count++ |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + println('Part 1: ${part1_count}') |
| 89 | + |
| 90 | + // Part 2 |
| 91 | + mut total_removed := 0 |
| 92 | + mut current_grid := grid2.clone() |
| 93 | + mut changed := true |
| 94 | + |
| 95 | + mut to_check := []Pos{cap: rows * cols} |
| 96 | + |
| 97 | + for i in 0 .. rows { |
| 98 | + for j in 0 .. cols { |
| 99 | + to_check << Pos{i, j} |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + for changed { |
| 104 | + changed = false |
| 105 | + mut to_remove := []Pos{cap: to_check.len} |
| 106 | + mut next_to_check := []Pos{cap: rows * cols} |
| 107 | + mut modified := []bool{len: rows * cols, init: false} |
| 108 | + |
| 109 | + for pos in to_check { |
| 110 | + i := pos.row |
| 111 | + j := pos.col |
| 112 | + idx := i * cols + j |
| 113 | + |
| 114 | + if current_grid[idx] != `@` { |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + adj_count := count_adjacent(current_grid, rows, cols, i, j, neighbors) |
| 119 | + if adj_count < 4 { |
| 120 | + to_remove << pos |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Remove rolls |
| 125 | + if to_remove.len > 0 { |
| 126 | + changed = true |
| 127 | + total_removed += to_remove.len |
| 128 | + |
| 129 | + // Mark positions as removed and track their neighbors |
| 130 | + for pos in to_remove { |
| 131 | + i := pos.row |
| 132 | + j := pos.col |
| 133 | + idx := i * cols + j |
| 134 | + current_grid[idx] = `.` |
| 135 | + modified[idx] = true |
| 136 | + |
| 137 | + // Add neighbors to next iteration's check list |
| 138 | + for offset in neighbors { |
| 139 | + ni := i + offset.dr |
| 140 | + nj := j + offset.dc |
| 141 | + if ni >= 0 && nj >= 0 && ni < rows && nj < cols { |
| 142 | + nidx := ni * cols + nj |
| 143 | + if !modified[nidx] { |
| 144 | + modified[nidx] = true |
| 145 | + next_to_check << Pos{ni, nj} |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + to_check = next_to_check.clone() |
| 152 | + } else { |
| 153 | + // No more to remove |
| 154 | + break |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + println('Part 2: ${total_removed}') |
| 159 | +} |
0 commit comments