use count instead of reject, this avoids storing the matching neighbours

This commit is contained in:
onyx-and-iris 2025-12-04 17:51:47 +00:00
parent 45c6ec52b0
commit b1957249e1

View File

@ -12,24 +12,25 @@ class Main
@sum = 0
removed_sum = 1
grid = Grid.new(data)
until removed_sum == 0
removed_sum = 0
loop do
grid_updated = false
(0...grid.rows.size).each do |row_index|
(0...grid.columns.size).each do |column_index|
next unless grid.cell_at(row_index, column_index).value == "@"
cell = grid.cell_at(row_index, column_index)
next unless cell.value == "@"
memo = grid.cell_at(row_index, column_index).neighbours.reject do |cell|
grid.cell_at(cell.row, cell.column).value != "@"
end
if memo.size < 4
if cell.neighbours.count do |neighbour|
neighbour.value == "@"
end < 4
data[row_index][column_index] = "x"
removed_sum += 1
@sum += 1
grid_updated = true
end
end
end
@sum += removed_sum
break unless grid_updated
grid = Grid.new(data)
end