Skip to content

Instantly share code, notes, and snippets.

@dukeimg
Last active July 30, 2016 09:58
Show Gist options
  • Save dukeimg/2c9adfd59efca16a6418ff423c72387d to your computer and use it in GitHub Desktop.
Save dukeimg/2c9adfd59efca16a6418ff423c72387d to your computer and use it in GitHub Desktop.
APU: Init Phase
STDOUT.sync = true # DO NOT REMOVE
# Don't let the machines win. You are humanity's last hope...
@width = gets.to_i # the number of cells on the X axis
@height = gets.to_i # the number of cells on the Y axis
@cells = Array.new
@height.times do
line = gets.chomp # width characters, each either 0 or .
@cells << line.split('') # заполняем ряды ячейками
end
STDERR.puts @cells
def node(cell) # проверяем ячейку на узел
if cell == '0'
true
else
false
end
end
def check(row, cell)
STDERR.puts row
if node(@cells[row][cell]) # ищем узел
coord1 = cell.to_s + ' ' + row.to_s # записываем его координату
if (cell + 1) == @width # если ячейка последняя в ряде
coord2 = '-1 -1' # то у неё нет соседа справа
else
1.upto(@width - cell) do |shift| # просматриваем ряд до конца в поисках узла
# shif - смезение поиска
if node(@cells[row][cell + shift])
coord2 = (cell + shift).to_s + ' ' + row.to_s
break # выход из цикла поиска
end
end
end
if coord2 == nil # если ничего не нашли
coord2 = '-1 -1'
end
if (row + 1) == @height # если ниже уже некуда
coord3 = '-1 -1'
else
1.upto(@height - row) do |shift| # просматриваем столбец
if @cells[row + shift] == nil # ряда нет
coord3 = '-1 -1'
break
elsif node(@cells[row + shift][cell])
coord3 = cell.to_s + ' ' + (row + shift).to_s
break
end
end
end
puts coord1 + ' ' + coord2 + ' ' + coord3
end
end
# row - номер ряда ряд
# cell - номер ячейки
@height.times do |row|
@width.times do |cell|
check(row, cell)
end
end
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
# Three coordinates: a node, its right neighbor, its bottom neighbor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment