Created
May 15, 2014 13:43
-
-
Save coding-brigadier/c912317ece9d4044e240 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def fd(canvas, pos_x, pos_y, units, dir) | |
units.times do | |
pos_y = if dir == "N" || dir == "NE" || dir == "NW" | |
pos_y - 1 | |
elsif dir == "S" || dir == "SE" || dir == "SW" | |
pos_y + 1 | |
else | |
pos_y | |
end | |
pos_x = if dir == "E" || dir == "NE" || dir == "SE" | |
pos_x + 1 | |
elsif dir == "W" || dir == "NW" || dir == "SW" | |
pos_x - 1 | |
else | |
pos_x | |
end | |
canvas[pos_y][pos_x] = "X" | |
end | |
[pos_x, pos_y] | |
end | |
def bk(canvas, pos_x, pos_y, units, dir) | |
units.times do | |
pos_y = if dir == "N" || dir == "NE" || dir == "NW" | |
pos_y + 1 | |
elsif dir == "S" || dir == "SE" || dir == "SW" | |
pos_y - 1 | |
else | |
pos_y | |
end | |
pos_x = if dir == "E" || dir == "NE" || dir == "SE" | |
pos_x - 1 | |
elsif dir == "W" || dir == "NW" || dir == "SW" | |
pos_x + 1 | |
else | |
pos_x | |
end | |
canvas[pos_y][pos_x] = "X" | |
end | |
[pos_x, pos_y] | |
end | |
def rt(degrees, current_direction) | |
transitions = { "N" => "NE", "NE" => "E", "E" => "SE", "SE" => "S", "S" => "SW", "SW" => "W", "W" => "NW", "NW" => "N" } | |
if degrees < 0 | |
until degress >= 0 | |
degress += 180 | |
end | |
elsif degrees >= 180 | |
until degrees < 180 do | |
degrees -= 180 | |
end | |
end | |
(degrees / 45).times do | |
current_direction = transitions[current_direction] | |
end | |
current_direction | |
end | |
def lt(degrees, current_direction) | |
rt(180 - degrees, current_direction) | |
end | |
size = gets.to_i | |
gets | |
canvas = [] | |
size.times do | |
row = [] | |
size.times do | |
row << '.' | |
end | |
canvas << row | |
end | |
pos_x = (size - 1) / 2 | |
pos_y = (size - 1) / 2 | |
dir = "N" | |
canvas[pos_y][pos_x] = 'M' | |
until (instr = gets).nil? | |
command = instr.split(" ").first | |
if command == "FD" || command == "BK" | |
units = instr.split(" ").last.to_i | |
pos_x, pos_y = (command == "FD") ? fd(canvas, pos_x, pos_y, units, dir) : bk(canvas, pos_x, pos_y, units, dir) | |
elsif command == "RT" || command == "LT" | |
degrees = instr.split(" ").last.to_i | |
dir = (command == "RT") ? rt(degrees, dir) : lt(degrees, dir) | |
else | |
elements = instr.split(" ") | |
times = elements[1].to_i | |
index = 3 | |
instructions = [] | |
until elements[index] == ']' | |
instructions << [elements[index], elements[index + 1]] | |
index += 2 | |
end | |
times.times do | |
instructions.each do |inst| | |
if inst.first == "FD" || inst.first == "BK" | |
units = inst.last.to_i | |
pos_x, pos_y = (inst.first == "FD") ? fd(canvas, pos_x, pos_y, units, dir) : bk(canvas, pos_x, pos_y, units, dir) | |
elsif inst.first == "RT" || inst.first == "LT" | |
degrees = inst.last.to_i | |
dir = (inst.first == "RT") ? rt(degrees, dir) : lt(degrees, dir) | |
end | |
end | |
end | |
end | |
end | |
canvas.each do |row| | |
puts row.join(" ") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment