Blindfolded Arithmetic/compile from Minsky machine
This python program by User:Jan jelo compiles Minsky machine program into Blindfolded Arithmetic program(state 0 means halt,and program starts from state 1).
Basically,a
encodes two counters in 2^A*3^B
form(It is initialized to 1 by the program),c
is used as an instruction pointer(Also initialized to 1 by the program.),i
is used to generate constants and store intermediate results(Non-zero input is required),other registers are used to store intermediate results.
Test if x and y are equal by 1/((x-y)*(x-y)+1)
(This is used to compare the instruction pointer with a constant).
Test if counter A is equal to 0 by a-a/2*2
,test if counter B is equal to 0 by (1+a-a/3*3)/2
.
Increase counter A by a=a*2
,decrease counter A by a=a/2
.
Increase counter B by a=a*3
,decrease counter B by a=a/3
.
x=[cond]?y:z
is equivalent to x=y*[cond]+z*(1-[cond])
.
Output will be the value of a
when the program halts.
program=''' inc a 2 inc a 3 inc a 4 inc b 5 dec a 4 0 ''' def compile(program): p=[[x for x in x.split(' ') if x in{'inc','dec','a','b'}or x.isdigit()] for x in program.split('\n')if x!=''] res='i=i/i\nd=a*a\nd=d+i\nd=i/d\na=a+d\nd=c*c\nd=d+i\nd=i/d\nc=c+d\n' #i=1;a=1 if a==0 else a;c=1 if c==0 else c i=1 #test if x==y:1/((x-y)*(x-y)+1) #test if x==0:1/(x*x+1) #test if A==0:a-a/2*2 #test if B==0:(1+(a-a/3*3))/2 #inc A:a=a*2 #inc B:a=a*3 #dec A:a=a/((A!=0)+1) #dec B:a=a/((B!=0)*2+1) for x in p: if(x[0],x[1])==('inc','a'): res+='d=c*i\n'+i*'d=d-i\n'+'d=d*d\nd=d+i\nd=i/d\nd=d+i\n' # d = (c=={i})+1 res+='a=a*d\n' # inc A if d==2 res+='d=d-i\nd=i-d\nc=c*d\nd=1-d\n'+int(x[2])*'c=c+d\n' # c = x[2] if d else c res+='i=a*i\ni=c/c\n' # exit if c==0 if(x[0],x[1])==('inc','b'): res+='d=c*i\n'+i*'d=d-i\n'+'d=d*d\nd=d+i\nd=i/d\ni=i+i\nd=d*i\ni=i/i\nd=d+i\n' # d = (c=={i})*2+1 res+='a=a*d\n' # inc B if d==3 res+='d=d-i\ni=i+i\nd=d/i\ni=i/i\nd=i-d\nc=c*d\nd=1-d\n'+int(x[2])*'c=c+d\n' # c = x[2] if d else c res+='i=a*i\ni=c/c\n' # exit if c==0 if(x[0],x[1])==('dec','a'): res+='d=c*i\n'+i*'d=d-i\n'+'d=d*d\nd=d+i\nd=i/d\n' # d = (c=={i}) res+='i=i+i\nb=a/i\nb=b*i\ni=i/i\nb=a-b\nb=i-b\n' # b = (A!=0) res+='b=b+i\na=a/b\nb=b-i\n' # inc A res+='e=e-e\n'+int(x[2])*'e=e+b\n'+'b=i-b\ni=i-i\n'+int(x[3])*'i=i+b\n'+'e=e+i\ni=a/a\ne=e*d\nd=i-d\ni=c*d\nc=e+i\ni=a/a\n' # c =(x[2]if b else x[3])if d else c res+='i=a*i\ni=c/c\n' # exit if c==0 if(x[0],x[1])==('dec','b'): res+='d=c*i\n'+i*'d=d-i\n'+'d=d*d\nd=d+i\nd=i/d\n' # d = (c=={i}) res+='b=i*i\ni=i+i\ni=i+b\nb=a/i\nb=b*i\ni=i/i\nb=a-b\nb=b+i\ni=i+i\nb=b/i\ni=i/i\nb=i-b\n' # b = (B!=0) res+='i=i+i\nb=b*i\ni=i/i\nb=b+i\na=a/b\nb=b-i\ni=i+i\nb=b/i\ni=i/i\n' # inc B res+='e=e-e\n'+int(x[2])*'e=e+b\n'+'b=i-b\ni=i-i\n'+int(x[3])*'i=i+b\n'+'e=e+i\ni=a/a\ne=e*d\nd=i-d\ni=c*d\nc=e+i\ni=a/a\n' # c =(x[2]if b else x[3])if d else c res+='i=a*i\ni=c/c\n' # exit if c==0 i+=1 return res a=compile(program) r''' a=a.replace('/','//') a=map(lambda x:f' {x}\n',a.split('\n')) a='a,b,c,d,e=[0]*5;i=int(input())\ntry:\n while True:\n'+''.join(a)+'except:\n print(i)' ''' print(a)
(Commenting out raw string quotation marks will produce a python code equivalent to Blindfolded Arithmetic code.)
For this example,the output will be:
i=i/i d=a*a d=d+i d=i/d a=a+d d=c*c d=d+i d=i/d c=c+d d=c*i d=d-i d=d*d d=d+i d=i/d d=d+i a=a*d d=d-i d=i-d c=c*d d=1-d c=c+d c=c+d i=a*i i=c/c d=c*i d=d-i d=d-i d=d*d d=d+i d=i/d d=d+i a=a*d d=d-i d=i-d c=c*d d=1-d c=c+d c=c+d c=c+d i=a*i i=c/c d=c*i d=d-i d=d-i d=d-i d=d*d d=d+i d=i/d d=d+i a=a*d d=d-i d=i-d c=c*d d=1-d c=c+d c=c+d c=c+d c=c+d i=a*i i=c/c d=c*i d=d-i d=d-i d=d-i d=d-i d=d*d d=d+i d=i/d i=i+i d=d*i i=i/i d=d+i a=a*d d=d-i i=i+i d=d/i i=i/i d=i-d c=c*d d=1-d c=c+d c=c+d c=c+d c=c+d c=c+d i=a*i i=c/c d=c*i d=d-i d=d-i d=d-i d=d-i d=d-i d=d*d d=d+i d=i/d i=i+i b=a/i b=b*i i=i/i b=a-b b=i-b b=b+i a=a/b b=b-i e=e-e e=e+b e=e+b e=e+b e=e+b b=i-b i=i-i e=e+i i=a/a e=e*d d=i-d i=c*d c=e+i i=a/a i=a*i i=c/c
and the program should output 81(3^4).