diff --git a/turing_machine.py b/turing_machine.py index fac9d4e..35f3f99 100644 --- a/turing_machine.py +++ b/turing_machine.py @@ -22,13 +22,23 @@ class TuringMachine: blank_symbol: Symbol = " " def run( - self, tape_input: str, max_steps: int = 10_000, verbose: bool = True + self, + tape_input: str, + head: int = 0, + max_steps: int = 10_000, + verbose: bool = True, ) -> Tuple[State, Tape]: tape: Tape = list(tape_input) + [self.blank_symbol] * 20 - head = 0 state = self.start_state steps = 0 + # normalize head position + if head < 0: + tape = [self.blank_symbol] * (-head) + tape + head = 0 + elif head >= len(tape): + tape += [self.blank_symbol] * (head - len(tape) + 1) + def debug_print(): tape_view = "".join(tape).replace(self.blank_symbol, "_") head_marker = " " * head + "^" @@ -109,6 +119,81 @@ tm = TuringMachine( reject_state="reject", ) -final_state, final_tape = tm.run("00110") +tape_input = "00110" +final_state, final_tape = tm.run(tape_input) print("Final State:", final_state) -print("Final Tape:", "".join(final_tape).strip()) +final_tape_str = "".join(final_tape).strip() +print("Final Tape:", final_tape_str) + +classic: TransitionFunction = {} + +# ok idk + +classic[("want_copy", "1")] = ("past_og_1", "X", "R") +classic[("want_copy", "0")] = ("past_og_0", "Y", "R") + +for sym in ["0", "1"]: + classic[("past_og_1", sym)] = ("past_og_1", sym, "R") + classic[("past_og_0", sym)] = ("past_og_0", sym, "R") + +classic[("past_og_1", " ")] = ("copy_1", " ", "R") +classic[("past_og_0", " ")] = ("copy_0", " ", "R") + +for sym in ["0", "1"]: + classic[("copy_1", sym)] = ("copy_1", sym, "R") + classic[("copy_0", sym)] = ("copy_0", sym, "R") + +classic[("copy_1", " ")] = ("back", "1", "L") +classic[("copy_0", " ")] = ("back", "0", "L") + +for sym in ["0", "1", " "]: + classic[("back", sym)] = ("back", sym, "L") + +for sym, target in [("X", "1"), ("Y", "0")]: + classic[("back", sym)] = ("want_copy", target, "R") + +classic[("want_copy", " ")] = ("accept", " ", "R") + +tm_classic = TuringMachine( + transition_function=classic, + start_state="want_copy", + accept_state="accept", + reject_state="reject", +) + +final_state, final_tape = tm_classic.run(tape_input) +print("Final State (Classic):", final_state) +final_tape_str = "".join(final_tape).strip() +print("Final Tape (Classic):", final_tape_str) + +# for now it will copy with a blank in between. so 00110 → 00110 00110, not 0011000110. but it works. and the other one works too. so that's good enough for now. + +left_shift_1: TransitionFunction = {} + +left_shift_1[("want_copy", "1")] = ("copy_1", " ", "L") +left_shift_1[("want_copy", "0")] = ("copy_0", " ", "L") +left_shift_1[("want_copy", " ")] = ("accept", " ", "R") + +for sym, target in [("copy_0", "0"), ("copy_1", "1")]: + # override for now, i can make a wrapping one but i wont + left_shift_1[(sym, "1")] = ("going_balls", target, "R") + left_shift_1[(sym, "0")] = ("going_balls", target, "R") + left_shift_1[(sym, " ")] = ("going_balls", target, "R") + +left_shift_1[("going_balls", " ")] = ("want_copy", " ", "R") + +left_shift_machine = TuringMachine( + transition_function=left_shift_1, + start_state="want_copy", + accept_state="accept", + reject_state="reject", +) + +left_shift_input = "101010" + +left_shift_final_state, left_shift_final_tape = left_shift_machine.run( + left_shift_input, head=1 +) +print("Final State (Left Shift):", left_shift_final_state) +left_shift_final_tape_str = "".join(left_shift_final_tape).strip() +print("Final Tape (Left Shift):", left_shift_final_tape_str)