Skip to content

Instantly share code, notes, and snippets.

@krishicks
Created March 7, 2011 03:12
Show Gist options
  • Save krishicks/858003 to your computer and use it in GitHub Desktop.
Save krishicks/858003 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
print "Input /^\\d \\d [NSEW] [LMR]+$/: "
raise "Bad input" unless input = gets.chomp.match(/^(\d+)\s(\d+)\s([NSEW])\s([LMR]+)$/)
x, y, direction, moves = input[1].to_i, input[2].to_i, input[3], input[4]
compass = %w{ N E S W }
i = compass.index(direction)
fns = lambda { y+=1 }, lambda { x+=1 }, lambda { y-=1 }, lambda { x-=1 }
play = { "L" => lambda { fns[i-1] ? i-=1 : i=-1 },
"R" => lambda { fns[i+1] ? i+=1 : i=0 },
"M" => lambda { fns[i].call }}
moves.split("").map { |move| play[move] }.map &:call
puts "#{x} #{y} #{compass[i]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment