mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-08 19:57:48 +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
|
@sum = 0
|
||||||
|
|
||||||
removed_sum = 1
|
|
||||||
grid = Grid.new(data)
|
grid = Grid.new(data)
|
||||||
until removed_sum == 0
|
loop do
|
||||||
removed_sum = 0
|
grid_updated = false
|
||||||
(0...grid.rows.size).each do |row_index|
|
(0...grid.rows.size).each do |row_index|
|
||||||
(0...grid.columns.size).each do |column_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|
|
if cell.neighbours.count do |neighbour|
|
||||||
grid.cell_at(cell.row, cell.column).value != "@"
|
neighbour.value == "@"
|
||||||
end
|
end < 4
|
||||||
if memo.size < 4
|
|
||||||
data[row_index][column_index] = "x"
|
data[row_index][column_index] = "x"
|
||||||
removed_sum += 1
|
@sum += 1
|
||||||
|
grid_updated = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@sum += removed_sum
|
|
||||||
|
break unless grid_updated
|
||||||
grid = Grid.new(data)
|
grid = Grid.new(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user