Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Last active December 26, 2015 17:29
Show Gist options
  • Save sharmaabhinav/7187312 to your computer and use it in GitHub Desktop.
Save sharmaabhinav/7187312 to your computer and use it in GitHub Desktop.
lines = IO.readlines("input.txt")
coordinates = lines[0]
max_x,max_y = lines[0].split(" ")
i = 1
def final_coordinates(x,y,d,steps)
x = x.to_i
y = y.to_i
direction_hash = { "N" => {"L" => "W","R" => "E"}, # hashmap to make decision when L or R is the instruction
"S" => {"L" =>"E","R"=>"W"} ,
"E" => {"L"=>"N","R"=>"S"} ,
"W" => {"L"=>"S","R"=>"N"}
}
steps.each_char{
|step|
# There will be two type of case 1 ) direction change without movement 2) movement without direction change
if step == 'M'
if d == "N"
y = y + 1
elsif d == "E"
x = x + 1
elsif d == "S"
y = y - 1
else
x = x - 1
end
else
d = direction_hash[d][step]
end
#print x,y,d," "
}
return x,y,d
end
count = 1
while i < lines.length
x,y,d = lines[i].split(" ")
steps = lines[i+1]
print "final coordinate of #{count} : ",final_coordinates(x,y,d,steps.split("\r")[0]) # remove /r /n from string
puts "\n"
i = i + 2
count = count + 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment