Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(3000)
log = open("D:/ctf/dicectf/log.txt", "w")
# -----
# This section is just used to implement stail-recursion.
# You probably don't need to reverse this but you can try if you want ;p
class TR(Exception):
SEEN = []
def __init__(self, key, args, kwargs):
self.key = key
self.args = args
self.kwargs = kwargs
def T(fn, name=''):
def _fn(*args, **kwargs):
key = id(_fn)
if key in TR.SEEN:
raise TR(key, args, kwargs)
else:
TR.SEEN.append(key)
while True:
try:
val = fn(*args, **kwargs)
TR.SEEN = TR.SEEN[:TR.SEEN.index(key)]
return val
except TR as e:
if e.key != key:
raise
else:
args = e.args
kwargs = e.kwargs
TR.SEEN = TR.SEEN[:TR.SEEN.index(key)+1]
return _fn
# -----
# Sice machine:
xpr = print
def print(*args, **kwargs):
xpr(*args, **kwargs, file=log, flush=True)
return False
xapply=lambda x:lambda b,**a:x(*b,**a)
sfirst=xapply(lambda x,*b:x)
stail=xapply(lambda x,*b:b)
first=xapply(lambda x,b:x)
last=xapply(lambda x,b:b)
make_tuple=lambda *x:x
# if x == () => return a()
# else return b()
iif=lambda x,b,a:sfirst(stail(make_tuple(*(((),)*(x==())),a,b)))()
make1_tuple=lambda x:(x,)
# make_nested_paren(n):
# n = 0 => return (), 1 => return ((),), 2=> return (((),),) and so on
make_nested_paren=xapply(lambda *x,a=():iif(x,lambda:make_nested_paren(stail(x),a=make1_tuple(a)),lambda:a))
# rol1: rotate left by 1
rol1=xapply(lambda *x:make_tuple(*stail(x),sfirst(x)))
# ii: apply func (a) n times, where n is the nesting level of x to b
ii = lambda x,b,a: iif(x,lambda:ii(sfirst(x),a(b),a),lambda:b)
# hh: apply a, len(x) times on b, tail recur
hh=T(lambda x,b,a:iif(x,lambda:hh(sfirst(x),a(b),a),lambda:b))
# rol1 n-1 times => rotate right 1
ror1=xapply(lambda *x:hh(sfirst(make_nested_paren(x)),x,rol1))
# one(l): put l[0] into n'th nested level where n = len(l)
# one([1,2,3]) = (((1,),),)
one=xapply(lambda *x:iif(stail(x),lambda:one(make_tuple(make1_tuple(sfirst(x)),*stail(stail(x)))),lambda:make1_tuple(sfirst(x))))
list_len=lambda x:hh(x,0,lambda b:b+1)
# dd: return a + map(b, x)
def fuck(tup, an=0):
if () == tup: return an
return fuck(tup[0], an+1)
def xfuck(tup):
r = fuck(tup)
return r
def dl(tp):
return [fuck(i) for i in tp]
dd=lambda x,b,a:iif(x,lambda:dd(stail(x),b,make_tuple(*a,b(sfirst(x)))),lambda:a)
# xmap: return map(b, x)
xmap=lambda x,b:dd(x,b,())
run_turing_machine = lambda x,b,a:(
iif(
first(sfirst(b)),
lambda: iif(
first(first(sfirst(b))),
lambda: (
print(f"ipop;dpush {xfuck(sfirst(a))}") or
(
make_tuple(
sfirst(a),
*stail(x)
),
rol1(b),
stail(a)
)
),
lambda: (
print(f"push {xfuck(sfirst(x))}") or
(
x, rol1(b), make_tuple(sfirst(x),*a)
)
)
),
lambda: iif(
first(last(sfirst(b))),
lambda: iif(
first(
first(last(sfirst(b)))
),
lambda: print(f'bufp += {fuck(sfirst(a))}') or (
(
ii(sfirst(a),x,rol1),
rol1(b),stail(a)
)
),
lambda: print(f'bufp -= {fuck(sfirst(a))}') or (
(
ii(sfirst(a),x,ror1),
rol1(b),stail(a)
)
),
),
lambda: iif(
first(last(last(sfirst(b)))),
lambda: iif(
first(
first(
last(last(sfirst(b)))
)
),
lambda: print(f'ip -= {fuck(sfirst(first(first(last(last(sfirst(b)))))))}') or (
x,
ii(
sfirst(
first(
first(
last(last(sfirst(b)))
)
)
),
b,
ror1
),
a
),
lambda: print(f'ip += ???') or (
x,
ii(
sfirst(
last(
first(
last(last(sfirst(b)))
)
)
),
b,
rol1
),
a
)
),
lambda: iif(
first(
last(
last(last(sfirst(b)))
)
),
lambda: iif(
first(
first(
last(
last(last(sfirst(b)))
)
)
),
lambda: print(f'pusha {fuck(sfirst(first(first(last(last(last(sfirst(b))))))))}') or (
x,
rol1(b),
make_tuple(
sfirst(
first(
first(
last(
last(
last(sfirst(b))
)
)
)
)
),
*a
)
),
lambda: (
x,
rol1(b),
make_tuple(
sfirst(
hh(
sfirst(
last(
first(
last(
last(last(sfirst(b)))
)
)
)
),
a,
rol1
)
),
*a
)
)
),
lambda: iif(
first(last(last(last(last(sfirst(b)))))),
lambda: iif(
first(first(last(last(last(last(sfirst(b))))))),
lambda: (
x,
iif(
sfirst(a),
lambda:rol1(b),
lambda:rol1(rol1(b))
),
stail(a)
),
lambda:(x,ii(sfirst(a),b,rol1),stail(a))
),
lambda: iif(
first(last(last(last(last(last(sfirst(b))))))),
lambda: iif(
first(first(last(last(last(last(last(sfirst(b)))))))),
lambda: print(f"add {fuck(sfirst(a))}, {fuck(sfirst(stail(a)))}") or (
x,
rol1(b),
make_tuple(
hh(sfirst(a),sfirst(stail(a)),make1_tuple),
*stail(stail(a))
)
),
lambda: print(f"sub {fuck(sfirst(stail(a)))}, {fuck(sfirst(a))}") or (
x,
rol1(b),
make_tuple(
hh(sfirst(a),sfirst(stail(a)),sfirst),
*stail(stail(a))
)
)
),
lambda:()
)
)
)
)
)
)
)
def loop(tape,code,str_input):
# while rip doesn't point to () => loop
# else halt the Turing Machine
while sfirst(code):
tape, code, str_input = run_turing_machine(tape, code, str_input)
return tape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment