Created
December 1, 2016 09:06
-
-
Save EricDykstra/cbbb9b87209814d93e5bb0d252646d0d 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
def change_state(xstate, d) | |
directions = "NESW" | |
i = "NESW".index(xstate) | |
if d == "R" | |
directions[i+1] ? directions[i+1] : "N" | |
else | |
directions[i-1] ? directions[i-1] : "W" | |
end | |
end | |
current = {x: 0, y: 0} | |
state = "N" | |
input.each do |d| | |
dir = d[0] | |
steps = d[1..-1] | |
steps = steps.to_i | |
state = change_state(state, dir) | |
if state == "N" | |
current[:y] = current[:y] + steps | |
elsif state == "E" | |
current[:x] = current[:x] + steps | |
elsif state == "S" | |
current[:y] = current[:y] - steps | |
elsif state == "W" | |
current[:x] = current[:x] - steps | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment