mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-07 22:57:50 +00:00
use count instead of reject, this avoids storing the matching neighbours
This commit is contained in:
parent
45c6ec52b0
commit
b1957249e1
21
day_04/2.rb
21
day_04/2.rb
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user