Last active
August 29, 2015 14:03
-
-
Save odaillyjp/7b190cc71ab7ee1e06d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/unit' | |
require 'minitest/autorun' | |
require 'pry' | |
MAX_WIDTH = 5 | |
MAX_HEIGHT = 5 | |
def solve(str) | |
init_numbers_field(str) | |
checked_field = init_checked_field | |
count_length = [] | |
MAX_WIDTH.times do |x| | |
MAX_HEIGHT.times do |y| | |
pos = check_rise_up_number(x, y, [], checked_field) | |
count_length << pos.size | |
end | |
end | |
count_length.max | |
end | |
def init_numbers_field(str) | |
@numbers_field = str.split('/').map(&:chars).transpose | |
end | |
def init_checked_field | |
checked_field = Array.new | |
MAX_WIDTH.times do | |
checked_field << Array.new(MAX_HEIGHT, false) | |
end | |
checked_field | |
end | |
def check_rise_up_number(x, y, pos, checked_field) | |
return pos if checked_field[x][y] == true | |
pos << [x, y] | |
checked_field[x][y] = true | |
current_number = @numbers_field[x][y].to_i | |
if (y > 0) and (@numbers_field[x][y-1].to_i > current_number) and !(checked_field[x][y-1]) | |
check_rise_up_number(x, y-1, pos, checked_field) | |
end | |
if (x < MAX_WIDTH - 1) and (@numbers_field[x+1][y].to_i > current_number) and !(checked_field[x+1][y]) | |
check_rise_up_number(x+1, y, pos, checked_field) | |
end | |
if (x > 0) and (@numbers_field[x-1][y].to_i > current_number) and !(checked_field[x-1][y]) | |
check_rise_up_number(x-1, y, pos, checked_field) | |
end | |
if (y < MAX_HEIGHT - 1) and (@numbers_field[x][y+1].to_i > current_number) and !(checked_field[x][y+1]) | |
check_rise_up_number(x, y+1, pos, checked_field) | |
end | |
pos | |
end | |
class TestFoo < MiniTest::Unit::TestCase | |
def test_exercise_no1 | |
assert_equal(solve('01224/82925/69076/32298/21065'), 6) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment