From 85d275f04b8d1bd4890f6908d0ec3e06009402c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 18 Aug 2023 22:34:02 +0000 Subject: [PATCH] helpers.rotate_r --- pigeon_magnet_solver/helpers.py | 36 +++++++++++++++++++++++++++++++++ tests/test_helpers.py | 17 ++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 pigeon_magnet_solver/helpers.py create mode 100644 tests/test_helpers.py diff --git a/pigeon_magnet_solver/helpers.py b/pigeon_magnet_solver/helpers.py new file mode 100644 index 0000000..d868189 --- /dev/null +++ b/pigeon_magnet_solver/helpers.py @@ -0,0 +1,36 @@ +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) diff --git a/tests/test_helpers.py b/tests/test_helpers.py new file mode 100644 index 0000000..8fe75c9 --- /dev/null +++ b/tests/test_helpers.py @@ -0,0 +1,17 @@ +from pigeon_magnet_solver import helpers + + +def test_rotate_r(): + assert helpers._rotate_r("1x01") == "11x0" + assert helpers._rotate_r("11x0") == "011x" + assert helpers._rotate_r("011x") == "x011" + assert helpers._rotate_r("x011") == "1x01" + + assert helpers._valid_rotate("1x01", "11x0") is False + assert helpers._valid_rotate("1x01", "011x") is False + assert helpers._valid_rotate("1x01", "x011") is True + assert helpers._valid_rotate("1x01", "1x01") is True + + assert helpers._fix_exes("1x01", "x011") == "0x11" + + assert helpers.rotate_r("1x01") == "0x11"