Last active
August 29, 2015 14:07
-
-
Save minoritea/27a877dfe108e9f96d2e to your computer and use it in GitHub Desktop.
Yokohama.rb Rotcell( pair-programming )
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
@initial_table = [ | |
%w[a b c d e], | |
%w[f g h i j], | |
%w[k l m n o], | |
%w[p q r s t], | |
%w[u v w x y] | |
] | |
MAX_X = 5 | |
MAX_Y = 5 | |
def init_table | |
@table = @initial_table.map{|row|row.dup} | |
end | |
def upcase?(str) | |
str =~ /[A-Z]/ | |
end | |
def location(v) | |
v = v.downcase | |
@table.each.with_index do |row, y| | |
row.each.with_index do |value, x| | |
return [y, x] if value == v | |
end | |
end | |
raise "hoge" | |
end | |
def range_of_x?(x) | |
(0...MAX_X).include? x | |
end | |
def range_of_y?(y) | |
(0...MAX_Y).include? y | |
end | |
def arround_slots y, x | |
order = [ | |
[y - 1, x - 1], [y - 1, x], [y - 1, x + 1], | |
[y, x + 1], | |
[y + 1, x + 1], [y + 1, x], [y + 1, x - 1], | |
[y, x - 1] | |
] | |
order.map do |(_y, _x)| | |
next unless range_of_y?(_y) | |
next unless range_of_x?(_x) | |
[_y, _x] | |
end.compact | |
end | |
def rotate v | |
y, x = location(v) | |
slots = arround_slots(y, x) | |
values = slots.map do |(y, x)| | |
@table[y][x] | |
end | |
if upcase?(v) | |
values.push values.shift | |
else | |
values.unshift values.pop | |
end | |
slots.each.with_index do |(y, x), i| | |
@table[y][x] = values[i] | |
end | |
values | |
end | |
def perform str | |
init_table | |
last_values = nil | |
str.each_char do |c| | |
last_values = rotate(c) | |
end | |
last_values.sort.join | |
end | |
puts perform(ARGV[0].strip) if ARGV[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment