Last active
February 28, 2024 16:18
-
-
Save i-am-unknown-81514525/853e88b713ad2c24d9e0c972090d99c8 to your computer and use it in GitHub Desktop.
Esoteric Code Generator (Convert python code to a one-linear with only symbol and 5 a-z character)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import traceback | |
from typing import Callable | |
COMPILING = 'hi.py' | |
OUT = 'out-hi.py' | |
# Normal style | |
repr_1 = '-~(()<())' | |
num_repr = { | |
0: f'({repr_1}>>{repr_1})', | |
1: f'{repr_1}', | |
2: f'({repr_1}<<{repr_1})', | |
3: f'({repr_1}+({repr_1}<<{repr_1}))', | |
4: f'({repr_1}<<{repr_1}<<{repr_1})', | |
5: f'(({repr_1}<<{repr_1}<<{repr_1})+{repr_1})', | |
6: f'(({repr_1}<<{repr_1})*(({repr_1}<<{repr_1})+{repr_1}))', | |
7: f'(({repr_1}<<{repr_1}<<{repr_1}<<{repr_1})-{repr_1})', | |
8: f'({repr_1}<<{repr_1}<<{repr_1}<<{repr_1})', | |
9: f'(({repr_1}+({repr_1}<<{repr_1}))**({repr_1}<<{repr_1}))', | |
10: f'({repr_1}+(({repr_1}+({repr_1}<<{repr_1}))**({repr_1}<<{repr_1})))' | |
} | |
func = { | |
"add": (lambda x, y: x + y, '({}+{})'), | |
"sub": (lambda x, y: x - y, '({}-{})'), | |
"pow": (pow, '({}**{})'), | |
"mul": (lambda x, y: x * y, '({}*{})'), | |
"bsl": (lambda x, y: x << y, '({}<<{})'), | |
} | |
cal_diff = lambda x, y: abs(x-y) | |
quick_mode = True | |
bsl_complete = False | |
bsr_complete = False | |
pow_complete = False | |
def gen_num(num:int=0) -> str: | |
limit = 1e10 | |
global num_repr, bsl_complete, bsr_complete, pow_complete | |
completed = False | |
diff = abs(num) | |
sqrt = int(math.sqrt(num)) | |
add_lim = 200 | |
if num == 0 or num in num_repr: | |
return (num_repr[num]) | |
while not completed: | |
for name, func_tup in func.items(): | |
if name == 'bsl': | |
if bsl_complete: | |
continue | |
bsl_complete = True | |
if name == 'bsr' and bsr_complete: | |
if bsr_complete: | |
continue | |
bsr_complete = True | |
if name == 'pow' and pow_complete: | |
if pow_complete: | |
continue | |
pow_complete = True | |
add_item = {} | |
set_num = set(num_repr) | |
for k0 in sorted(list(num_repr)): | |
if ((k0 > min(abs(num), 1000)) or (k0 < max(-abs(num), -1000))) and name in ['pow']: | |
continue | |
if abs(k0 - num) > abs(diff * 2): | |
continue | |
dis = limit | |
if name in ['bsr', 'bsl'] and k0 > 250: | |
break | |
if name in ['add', 'sub']: | |
if name == 'add': | |
new = num - k0 | |
if new in num_repr: | |
add_item[num] = func[name][1].format(num_repr[k0], num_repr[new]) | |
completed = True | |
break | |
elif name == 'sub': | |
new = k0 - num | |
if new < 0: | |
continue | |
if new in num_repr: | |
add_item[num] = func[name][1].format(num_repr[k0], num_repr[new]) | |
completed = True | |
break | |
if k0 > add_lim: | |
continue | |
if quick_mode: | |
if name == 'mul': | |
if k0 > 1000: | |
new = num//k0 | |
check = set(range(new-10, new+10)) & set_num | |
for v in check: | |
value = func[name][0](k0, v) | |
if value not in num_repr: | |
add_item[value] = func[name][1].format(num_repr[k0], num_repr[v]) | |
continue | |
for k1 in sorted(list(num_repr)): | |
if k1 > add_lim and name in ['add', 'sub']: | |
break | |
if abs(k1) > abs(diff * 2): | |
break | |
if (k1 < 0) and name in ['pow', 'bsl', 'bsr']: | |
continue | |
if (k1 > 20) and name in ['pow']: | |
break | |
if (k1 > 10) and name in ['bsr', 'bsl']: | |
break | |
if name in ['mul']: | |
if k1 > sqrt: | |
break | |
try: | |
result = func_tup[0](k0, k1) | |
except Exception as e: | |
traceback.print_exc() | |
continue | |
# print(f'Attempted {name} with {k0}, {k1}') | |
if diff > cal_diff(num, result) and result not in add_item and result not in num_repr: | |
add_item[result] = func_tup[1].format(num_repr[k0], num_repr[k1]) | |
if cal_diff(num, result) > diff: | |
if cal_diff(num, result) > dis: | |
break | |
dis = cal_diff(num, result) | |
if result == num: | |
completed = True | |
break | |
if completed: | |
break | |
num_repr = {**num_repr, **add_item} | |
if completed: | |
break | |
add_lim *= 2 | |
if completed: | |
return (num_repr.get(num)) | |
base_content = """_=eval;__='c';___=_((('%'+__)%(((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<())))*(-~(()<())+(-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<())))))))+(('%'+__)%((-~(()<())<<-~(()<())<<-~(()<())<<-~(()<()))*((-~(()<())+(-~(()<())<<-~(()<())))+(-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<())))))))+(('%'+__)%((((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<())))+(-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<())))))*((-~(()<())<<-~(()<()))*((-~(()<())<<-~(()<()))+-~(()<()))))));____=_(___((-~(()<())+((-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<()))))**(-~(()<())<<-~(()<())))))+___(((-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<()))))*((-~(()<())<<-~(()<()))+(-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<())))))))+___((-~(()<())+((-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<()))))**(-~(()<())<<-~(()<())))))+___((((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<())))*(-~(()<())+(-~(()<())+((-~(()<())+(-~(()<())<<-~(()<())))**(-~(()<())<<-~(()<()))))))));""" | |
append_from = "____({})" | |
per_char_append = "___({})" | |
connector = '+' | |
add_content = [] | |
with open(COMPILING, 'r') as fp: | |
for char in fp.read(): | |
add_content.append(per_char_append.format(gen_num(ord(char)))) | |
with open(OUT, 'w') as fp: | |
fp.write(base_content+append_from.format(connector.join(add_content))) |
This code have a known issue that it cannot convert code that more than ~1000 character as it would receive the following error:
RecursionError: maximum recursion depth exceeded during compilation
I have made a fixed version but decided to not upload it to prevent abuse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you wonder, this code is basically just powered by my esoteric numer generator and the only thing special is just the automatic conversion