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 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
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}"} |
output
1 3 N
5 1 E
There is some fat to trim, but I'm pretty sure it's not going to get down to 2 lines of code, using the almost_sinatra benchmark of a line of code being max 120 chars.
Updated to as small as I'm going to get it today.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i (the input file)