def generate_bf(self): # Layout input: [0, 0, 0, state, 0, symbol, 0, 0] # ^ # Layout output: [0, 0, 0, state, 0, 0, 0] # ^ cases = sorted(self.transitions.items()) n2n_12 = get_n2n((1, 2)) n2n_23 = get_n2n((2, 3)) current = 0 yield "<<+>>" for state, transition in cases: d = current - state yield n2n_23[d] current = state yield "[>]<<" # When current_state is zero, we point at a 1, otherwise at a 0 if True: # old state is state # Layout: [0, 1, 0, 0, 0, symbol, 0, 0] # ^ yield "[>>>>" # Now we point at symbol if True: # symbol == 1 yield "[-<<<<->>" yield n2n_12[transition[1][0]] yield ">" + transition[1][1] yield ">" # Layout: [0, 0, 0, state, 0, 0, 0] # ^ yield "<]" yield "<<<<" if True: # symbol == 0 yield "[->>" # Layout: [0, 0, 0, 0, 0, 0, 0, 0] # ^ yield n2n_12[transition[0][0]] yield ">" + transition[0][1] yield "<<<<]" # Layout: [0, 0, 0, state, 0, 0, 0] # ^ yield "]" yield ">" yield "[+]>>"
def modify_two(base_text: str): n2n_2 = get_n2n((1, 2), cache_dir="Number2Number") n2n_1 = get_n2n((-1, -2), cache_dir="Number2Number") yield ">>>" last_1, last_2 = ord(base_text[1]), ord(base_text[0]) yield n2n_1[last_1] yield ">" yield n2n_2[last_2] last = 2 for c in base_text: v = ord(c) if abs(v - last_1) <= abs(v - last_2): if last == 2: yield "<" yield n2n_1[v - last_1] + "." last_1 = v last = 1 else: if last == 1: yield ">" yield n2n_2[v - last_2] + "." last_2 = v last = 2
def from_huffman_tree(cls, root: HuffmanTree): self = cls() i = 0 n2n = get_n2n((1, 2)) def _rec(tree): nonlocal i state = i i += 1 for s, v in enumerate(tree): if isinstance(v, tuple): self.add(state, s, _rec(v)) else: self.add(state, s, 0, "" + n2n[ord(v)] + ".[-]") return state _rec(root) return self
def histogram_moves_repeated(text: str, n: int): single_weights = Counter(text) print(single_weights) pair_weights = Counter(frozenset(t) for t in zip(text, text[1:]) if t[0] != t[1]) pair_weights = {p: w for p, w in pair_weights.items() if w > 10} print(pair_weights) order = single_weights.most_common() print(order) thresholds = Counter({c: w // 10 for c, w in single_weights.items()}) count = Counter() while len(order) < n: (to_add, threshold), = thresholds.most_common(1) i = 0 for i, (_, w) in enumerate(order): if threshold > w: break order.insert(i, (to_add, threshold)) thresholds[to_add] = threshold // 10 print(order) base = len(order) // 2 print(base) layout: list[str] = [None] * len(order) for i, (c, _) in enumerate(order): layout[base + ((i // 2) if i % 2 == 0 else -i // 2)] = c print(layout) # _optimize_layout(layout, pair_weights, tuple(t[0] for t in single_weights.most_common(9))) _optimize_layout_list_random(layout, pair_weights, tuple(t[0] for t in single_weights.most_common())) # assert len(order) == len(single_weights) == len(set(order.values())) # pos_to_char = {i: c for c, i in order.items()} n2n = get_n2n((1, 2)) for i, c in enumerate(layout): yield cleanup(n2n[ord(c)] + ">") yield "<" current_cell = len(layout) - 1 layout = tuple(layout) for c in text: d = _find_closest(layout, current_cell, c) - current_cell yield forward(d) + "." current_cell += d
def histogram_moves(text: str): single_weights = Counter(text) print(single_weights) pair_weights = Counter(frozenset(t) for t in zip(text, text[1:]) if t[0] != t[1]) pair_weights = {p: w for p, w in pair_weights.items() if w > 10} print(pair_weights) base = len(single_weights) // 2 print(base) layout = {c: (base + ((i // 2) if i % 2 == 0 else -i // 2)) for (i, (c, _)) in enumerate(single_weights.most_common())} print(layout) # _optimize_layout(layout, pair_weights, tuple(t[0] for t in single_weights.most_common(9))) _optimize_layout_random(layout, pair_weights, tuple(t[0] for t in single_weights.most_common())) assert len(layout) == len(single_weights) == len(set(layout.values())) pos_to_char = {i: c for c, i in layout.items()} n2n = get_n2n((1, 2)) for i, c in sorted(pos_to_char.items()): yield cleanup(n2n[ord(c)] + ">") current_cell = len(pos_to_char) for c in text: d = layout[c] - current_cell yield forward(d) + "." current_cell = layout[c]
from number2number import get_n2n from simple_bf import Interpreter from statemachine2bf import BinaryStateMachine bsm = BinaryStateMachine() bsm.add(0, 0, 0, get_n2n((1, 2))[ord("A")] + ".[-]") bsm.add(0, 1, 1, get_n2n((1, 2))[ord("B")] + ".[-]") bsm.add(1, 0, 1, get_n2n((1, 2))[ord("C")] + ".[-]") bsm.add(1, 1, 0, get_n2n((1, 2))[ord("D")] + ".[-]") code = "".join(bsm.generate_bf()) # print(code) i = Interpreter(256) i.execute("+->>>>>+-<<+") i.print() print("-" * 100) i.execute(code) i.print()
def __post_init__(self): self._n2n = get_n2n((e - self.base for e in self.extras))
def modify_one(base_text: str): n2n = get_n2n((1, 2), cache_dir="Number2Number") last = 0 for c in base_text: yield n2n[ord(c) - last] + "." last = ord(c)
def linear_create(base_text: Iterable[int | str], after=".>"): n2n = get_n2n((1, 2), cache_dir="Number2Number") for c in base_text: yield n2n[ord(c) if not isinstance(c, int) else c] + after