Skip to content

Instantly share code, notes, and snippets.

@mesiriak
Last active April 16, 2021 09:38
Show Gist options
  • Save mesiriak/fb34f005272bd795c842a423129b0251 to your computer and use it in GitHub Desktop.
Save mesiriak/fb34f005272bd795c842a423129b0251 to your computer and use it in GitHub Desktop.
Codewars. Python
import re
def check(exp):
if len(exp) == 1 and exp[0] in ["r","l","R","L"]:
return f"{exp}0"
else:
return exp
def mutation(vector,y,x,val):
if vector == "right": x+= val
if vector == "left": x-= val
if vector == "top": y-= val
if vector == "bottom": y+= val
return y,x
y,x = 0,0
vector = "top"
keys = {
"top+r": "right",
"top+l": "left",
"right+r": "bottom",
"right+l": "top",
"bottom+r": "left",
"bottom+l": "right",
"left+r": "top",
"left+l": "bottom",
"left":"right","right":"left",
"top":"bottom","bottom":"top"
}
def i_am_here(path):
global y,x,vector
if len(re.findall(r"^\d*",path)[0]) > 0: y,x = mutation(vector,y,x,int(re.findall(r"^\d*",path)[0]))
actions = [*re.findall(r"[r|l|R|L]\d*",path)]
for i in range(len(actions)):
actions[i] = check(actions[i])
val = int(actions[i][1::])
if actions[i][0] == "r":
vector = keys[f"{vector}+r"]
y,x = mutation(vector,y,x,val)
if actions[i][0] == "l":
vector = keys[f"{vector}+l"]
y,x = mutation(vector,y,x,val)
if actions[i][0] in ["L","R"]:
vector = keys[vector]
y,x = mutation(vector,y,x,val)
return [y,x]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment