42 lines
802 B
Ruby
Executable File

#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "grid"
class Main
def run
data = []
$stdin.each_line do |line|
data << line.chomp
end
@sum = 0
grid = Grid.new(data)
loop do
grid_updated = false
(0...grid.rows.size).each do |row_index|
(0...grid.columns.size).each do |column_index|
cell = grid.cell_at(row_index, column_index)
next unless cell.value == "@"
if cell.neighbours.count do |neighbour|
neighbour.value == "@"
end < 4
data[row_index][column_index] = "x"
@sum += 1
grid_updated = true
end
end
end
break unless grid_updated
grid = Grid.new(data)
end
puts @sum
end
end
Main.new.run