Last active
August 29, 2015 14:07
-
-
Save joker1007/0a0df2579896ed24732c 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
#!/usr/bin/env ruby | |
require 'pp' | |
BASE_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), | |
] | |
RIGHT_ROTATE_TABLE = [ | |
[-1, -1], [-1, 0], [-1, 1], | |
[0, 1], | |
[1, 1], | |
[1, 0], | |
[1, -1], | |
[0, -1], | |
] | |
LEFT_ROTATE_TABLE = RIGHT_ROTATE_TABLE.reverse | |
MIN_INDEX = 0 | |
MAX_INDEX = BASE_TABLE[0].size - 1 | |
# BASE_TABLE[0][4] == e | |
def find_character(char) | |
BASE_TABLE.each_with_index do |row, row_count| | |
row.each_with_index do |col, col_count| | |
if col == char | |
return [row_count, col_count] | |
end | |
end | |
end | |
end | |
def around_points_of(direction, point) | |
results = [] | |
rotate_table = case direction | |
when :right | |
RIGHT_ROTATE_TABLE | |
when :left | |
LEFT_ROTATE_TABLE | |
end | |
rotate_table.each do |delta_row, delta_col| | |
if point[0] + delta_row < MIN_INDEX || | |
point[0] + delta_row > MAX_INDEX || | |
point[1] + delta_col < MIN_INDEX || | |
point[1] + delta_col > MAX_INDEX || | |
(delta_row == 0 && delta_col == 0) | |
else | |
results << [point[0] + delta_row, point[1] + delta_col] | |
end | |
end | |
results | |
end | |
# p around_points_of(:right, [0, 0]) | |
# p around_points_of(:left, [0, 1]) | |
# p around_points_of(:right, [2, 2]) | |
def rotate_table(direction, point) | |
targets = around_points_of(direction, point) | |
targets.inject(BASE_TABLE[targets.last[0]][targets.last[1]]) do |pre, t| | |
store = BASE_TABLE[t[0]][t[1]].dup | |
BASE_TABLE[t[0]][t[1]] = pre | |
store | |
end | |
end | |
def parse_command(command) | |
command.each_char.map do |c| | |
if c == c.upcase | |
[:left, c.downcase] | |
else | |
[:right, c.downcase] | |
end | |
end | |
end | |
command = ARGV[0] | |
parse_command(command).each do |c| | |
rotate_table(c[0], find_character(c[1])) | |
end | |
puts around_points_of(:right, find_character(command.each_char.to_a.last.downcase)).map {|p| | |
BASE_TABLE[p[0]][p[1]] | |
}.sort.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment