Last active
December 14, 2015 18:59
-
-
Save cmaitchison/5133520 to your computer and use it in GitHub Desktop.
mars rover problem in 3 lines of ruby 2 code
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
IO.read('i').lines.drop(1).each_slice(2){|p,t|x,y,h=p.split;x,y,h=[x.to_i,y.to_i,h.to_sym];z={N:[->{y+=1},:W,:E],E:[->{ | |
x+=1},:N,:S],S:[->{y-=1},:E,:W],W:[->{x-=1},:S,:N]};t.strip.split(//).map{|c|if c=='M';z[h][0].call;elsif c=='L';h= | |
z[h][1];else;h=z[h][2];end};puts"#{x} #{y} #{h}"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The challenge: https://twitter.com/HeyHeySueEllen/status/311051492108939265
And a description of the problem, for those that are interested: http://www.technicalypto.com/2010/02/thoughtworks-mass-rovers-coding-problem.html