Skip to content

Instantly share code, notes, and snippets.

@dukeimg
Last active April 26, 2016 13:42
Show Gist options
  • Save dukeimg/11e338816f6e44230b0f77c79895ffe0 to your computer and use it in GitHub Desktop.
Save dukeimg/11e338816f6e44230b0f77c79895ffe0 to your computer and use it in GitHub Desktop.
APU redux
require './lib/scheme'
describe Scheme do
describe '.check' do
let(:scheme) { Scheme.new(:height => 4, :width => 4,
:cells => [
['0', '0', '.', '0'],
['0', '.', '0', '0'],
['.', '0', '.', '0'],
['0', '0', '0', '.']
]) }
it 'finds close nodes' do
expect(scheme.check(0, 0)).to eq('0 0 1 0 0 1')
end
it 'finds distanced nodes' do
expect(scheme.check(1, 0)).to eq('0 1 2 1 0 3')
end
it 'handling none neighbors' do
expect(scheme.check(3, 2)).to eq('2 3 -1 -1 -1 -1')
end
it 'does not exist' do
expect(scheme.check(3, 3)).to eq(nil)
end
end
end
require './lib/scheme'
require './lib/input_output_handler'
app = InputOutputHandler.new
app.input
Scheme.new(:height => app.height,
:width => app.width,
:cells => app.cells).check_all.each do |row|
app.output(row)
end
class InputOutputHandler
attr_reader :cells, :width, :height
def initialize
@width = 0
@height = 0
@cells = Array.new
end
def input
print 'width: '
@width = gets.to_i
print 'height: '
@height = gets.to_i
puts "Enter cells in the row WITHOUT spaces. Node = '0' Empty cell = '.'"
height.times do |n|
print "row #{n + 1}: "
line = gets.chomp # width characters, each either 0 or .
cells << line.split('') # заполняем ряды ячейками
end
end
def output(data)
puts data
end
end
class Scheme
attr_accessor :height, :width, :cells
def initialize(options={})
@height = options[:height]
@width = options[:width]
@cells = options[:cells]
end
def node(cell) # проверяем ячейку на узел
if cell == '0'
true
else
false
end
end
def coord1(row, cell)
return "#{cell.to_s} #{row.to_s}" if node(@cells[row][cell]) # возврат координаты ячейки, если она существует
end
def coord2(row, cell)
if (cell + 1) == @width # если ячейка последняя в ряде
coord2 = '-1 -1' # то у неё нет соседа справа
else
1.upto(@width - cell) do |shift| # просматриваем ряд до конца в поисках узла
# shift - смещение поиска
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
coord2
end
def coord3(row, cell)
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
coord3
end
def check_all
result = Array.new
@height.times do |row|
@width.times do |cell|
result << check(row, cell)
end
end
result
end
def check(row, cell)
unless coord1(row, cell) == nil
"#{coord1(row, cell)} #{coord2(row, cell)} #{coord3(row, cell)}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment