pigeon-magnet-solver/pigeon_magnet_solver/helpers.py

36 lines
694 B
Python

def _rotate_r(__s: str) -> str:
return __s[-1] + __s[0:-1]
def _valid_rotate(__s: str, __c: str) -> bool:
for sc, cc in zip(__s, __c):
# can never rotate a "1" onto an "x"
if sc == "x" and cc == "1":
return False
return True
def _fix_exes(__s: str, __c: str) -> str:
result = ""
for sc, cc in zip(__s, __c):
if sc == "x":
result += "x"
elif cc == "x":
result += "0"
else:
result += cc
return result
def rotate_r(__s: str) -> str:
result = _rotate_r(__s)
while not _valid_rotate(__s, result):
result = _rotate_r(result)
return _fix_exes(__s, result)