Skip to content

Instantly share code, notes, and snippets.

@EricDykstra
Created December 1, 2016 09:06
Show Gist options
  • Save EricDykstra/cbbb9b87209814d93e5bb0d252646d0d to your computer and use it in GitHub Desktop.
Save EricDykstra/cbbb9b87209814d93e5bb0d252646d0d to your computer and use it in GitHub Desktop.
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