Created
May 18, 2018 13:41
-
-
Save satos---jp/45cb82b84af41e8cc9c78dd02b07e68b to your computer and use it in GitHub Desktop.
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
def eval(s,inputs): | |
mem = [0 for i in xrange(6000)] | |
ip = 0 | |
mp = 0 | |
stack = [] | |
def state2s(): | |
return ( | |
''.join(map(lambda (i,c): "*"+c if i == ip else c ,enumerate(s))) + '\n' + | |
str(map(lambda (i,c): "@"+str(c) if i == mp else c ,enumerate(mem[:200]))) | |
) | |
while ip < len(s): | |
#print ip,s[ip],mem | |
if s[ip]=='+': | |
mem[mp] = (mem[mp] + 1) % 256 | |
elif s[ip]=='-': | |
mem[mp] = (mem[mp] + 255) % 256 | |
elif s[ip]=='>': | |
mp += 1 | |
elif s[ip]=='<': | |
mp -= 1 | |
elif s[ip]=='[': | |
if mem[mp]==0: | |
d = 1 | |
while d > 0: | |
ip += 1 | |
if s[ip]=='[': | |
d += 1 | |
elif s[ip]==']': | |
d -= 1 | |
else: | |
stack += [ip] | |
elif s[ip]==']': | |
ip = stack[-1]-1 | |
stack = stack[:-1] | |
elif s[ip]==',': | |
mem[mp] = ord(inputs[0]) | |
inputs = inputs[1:] | |
elif s[ip]=='.': | |
print chr(mem[mp]), | |
elif s[ip]=='@': | |
#print ip,mp | |
#print state2s() | |
""" | |
vs = [] | |
for i in range(40): | |
k = '' | |
for j in range(6): | |
k = chr(mem[i*49+j*7+8+1]+48) + k | |
vs.append(k) | |
for i in range(12): | |
print [x for x in enumerate(vs)][i*3:i*3+3] | |
""" | |
#print '' | |
pass | |
ip += 1 |
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
#coding: utf-8 | |
from Tkinter import Tk | |
import bf_interpreter | |
import re | |
r = Tk() | |
r.clipboard_clear() | |
# 各桁に5個くらいスペースがほしい。 | |
def comp(s): | |
s = ''.join(map(lambda s: s.split('#')[0],s.split('\n'))) | |
#print s | |
s = ''.join(re.split('\s',s)) | |
#print s | |
s = re.split('(\d*.)',s) | |
#print s | |
s = ''.join(map(lambda x: (x if len(x)<=1 else x[-1] * int(x[:-1])),s)) | |
#print s | |
return s | |
# [keta x | |
def norm(): | |
# [h' x 0 0 0 0] | |
res = ''' | |
[ | |
>>+[ | |
# [h x 1' 0 0 0] | |
-< | |
# [h x' 0 0 0 0] | |
[>+>+<<-]>[<+>-] 10+ # [h x 10' x 0 0 0] | |
[ | |
# [h x d' dx 0 0 b] | |
>[>+>+<<-]+ # [h x d 1' dx dx b] | |
>[<->[-]]< # [h x d (if dx then 0 else 1)' 0 dx b] | |
[>>>+<<<-] # [h x d 0' 0 dx (b |= dx == 0)] | |
>>[<<+>>-] # [h x d dx 0 0' (b |= dx == 0)] | |
<<-<- | |
] | |
# [h x 0' (x-10) 0 0 b] | |
>[-] | |
# [h x 0 0' 0 0 (x < 10)] | |
>>+>[<->[-]]< | |
# [h x 0 0 0 (x >= 10)' 0] | |
[- | |
<<<< 10- | |
# [h x-=10' 0 0 0 0 0] | |
#次の桁をincrementさせる | |
7>+7< | |
>+>>> | |
] | |
# [h x iscont 0 0 0' 0] | |
<<< | |
] | |
# [h x%10 0' 0 0 0 0] | |
<<7> | |
] | |
7<[7<]7> | |
''' | |
return comp(res) | |
def putd(): | |
return comp(''' | |
[7>]7< | |
[> >8+[<6+>-]< .<7<] | |
''') | |
#s = norm(0) | |
np = 0 | |
# [keta, x, point] | |
# s[1] += s[0]. s[1] と s[0] は破壊される。 | |
# 位置はs[0] から s[1] になる。 | |
def add(): | |
global np | |
np = np + 1 | |
return comp(''' | |
>>+<<[7>]7> >>+7<-[+7<-]+<< | |
#各数字の先頭に+1をおく。 | |
# @1'0 @1 | |
[> | |
# @1'1 @1 | |
[>7>-[+7>-]<+>+7<-[+7<-]+<-] | |
#ひとつめの数字の桁についての加算 | |
>7>-[+7>-]7>+7<-[+7<-] 7>+2< | |
# @2'0 @2 | |
#桁をずらす | |
] | |
>>--[+7>-]<<7<[7<]7> | |
''') + norm() | |
# s[0] = - s[0]. | |
#補数を計算する | |
def hosuu(): | |
return comp(''' | |
# h', x, 0 | |
[ | |
>[>+<-]9+>[<->-] | |
# h, 9-x, 0' | |
<<7> | |
] | |
7<[7<]7>>+< | |
''') + norm() | |
# s[2] = s[1] * s[0]. | |
""" | |
p = 0 | |
rp = 0 | |
while p < len(s[0]): | |
# 0 -> 2o | |
r = rp | |
q = 0 | |
# 1 -> 2i | |
while r < len(s[2]): | |
#s[2][r] += s[1][q] * s[0][p] | |
# 2i -> 0 | |
copy(s[0][p]) | |
while s[0][p]>0: | |
# 0 -> 1 | |
copy(s[1][q]) | |
while s[1][q] >0: | |
# 1 -> 2i | |
s[2][r] += 1 | |
# 2i -> 1 | |
s[1][q] -= 1 | |
restore(s[1][q]) | |
# 1 -> 0 | |
s[0][p] -= 1 | |
restore(s[0][p]) | |
# 0 -> 1 | |
q += 1 | |
r += 1 | |
p += 1 | |
rp += 1 | |
""" | |
# s[0] [keta, x, 0, 外loop, 0, 0, 0] | |
# s[1] [keta, x, 0, 0, 内loop] | |
# s[2] [keta, x, 0, 0, 0, 内loop(r), 外loop(rp)] | |
def mult(): | |
global np | |
np += 2 | |
return (comp(''' | |
[7>]7>[7>]7>>>>>>>+ | |
# rp = 0 | |
>[7<]7<[7<]7<[7<]7>>>>+ | |
# p = 0 | |
<<< | |
# while p < len(s[0]): | |
[ | |
# s[2] のやつが外に出るまで | |
>>>>>>-[+7>-]+<+ | |
# r = rp | |
<<<<< [7<]7<[7<]7>>>>>+ | |
# q = 0 | |
>-[+7>-]+<<<<< | |
# while r < len(s[2]): | |
[ | |
>>>-[+7<-]+ <<[>+>>+<<<-]>>>[<<<+>>>-]<< | |
# copy(s[0][p]) | |
# s[0] :: [keta, x, x', 外loop, 0, 0, 0] | |
# while s[0][p]>0: | |
[ | |
>>-[+7>-]+ <<<[>+>+<<-]>>[<<+>>-]< | |
# s[1] :: [h, x, x', 0, 内loop] | |
#copy(s[1][q]) | |
# while s[1][q] >0: | |
[ | |
>>>-[+7>-]+<<<<+ | |
#s[2][r] += 1 | |
>>>-[+7<-]+<<- | |
#s[1][q] -= 1 | |
] | |
>-[+7<-]+<- | |
#s[0][p] -= 1 | |
] | |
>>-[+7>-] 7>+ | |
# q += 1 | |
>-[+7>-] 7>+ | |
# r += 1 | |
<<<<< | |
] | |
>>>>>-> | |
#rを消す | |
''') + '@' + | |
#ここ、normしてないとたぶんだめそう | |
comp('-[+7<-]<<<<<<+') + | |
#rpをketaに退避する | |
comp('[7<]7>') + norm() + | |
#正規化 | |
comp('@ --[++7>--]+>>>>>>+ @') + | |
#rpの復帰 | |
comp(''' @ | |
-[+7<-] 7>+ | |
# rp += 1 | |
<<-[+7<-] | |
# qを消す | |
< -[+7<-] 7>+ | |
# p += 1 | |
<<< | |
] | |
>>>- | |
#pを消す | |
>>>-[+7>-] | |
#rpを消す | |
<<<<<<7<[7<]7> | |
#a[2]の先頭に移る | |
''')) + norm() | |
# a[0] /= 6 | |
def div6(): | |
return comp(''' | |
[7>]7< | |
[ | |
>>+[ | |
# [h x 1' 0 0 0] | |
-< | |
# [h x' 0 0 0 0] | |
[>+>+<<-]>[<+>-] 6+ # [h x 6' x 0 0 0] | |
[ | |
# [h x d' dx 0 0 b] | |
>[>+>+<<-]+ # [h x d 1' dx dx b] | |
>[<->[-]]< # [h x d (if dx then 0 else 1)' 0 dx b] | |
[>>>+<<<-] # [h x d 0' 0 dx (b |= dx == 0)] | |
>>[<<+>>-] # [h x d dx 0 0' (b |= dx == 0)] | |
<<-<- | |
] | |
# [h x 0' (x-6) 0 0 b] | |
>[-] | |
# [h x 0 0' 0 0 (x < 6)] | |
>>+>[<->[-]]< | |
# [h x 0 0 0 (x >= 6)' 0] | |
[- | |
<<<< 6- | |
# [h x-=6' 0 0 0 0 0] | |
#前のとこをincrementさせる | |
<<+>> | |
>+>>> | |
] | |
# [h x iscont 0 0 0' 0] | |
<<< | |
] | |
# [x/6 h x%6 0' 0 0 0 0] | |
<[7<10+7>-] | |
#hの次のを繰り下げる | |
<<[>>+<<-] | |
#戻す | |
>7< | |
] | |
>[-]7>< | |
#手前のごみを消す | |
''') | |
""" | |
s = comp(''' | |
60> | |
6+<+6< 9+<+6< 5+<+ | |
''') + '@' + norm() + '@' + putd() | |
""" | |
s = comp(''' | |
60> | |
1+<+6< 2+<+6< 3+<+6< 7< 4+<+6< 5+<+6< 6+<+ | |
''') + add() + '@' + putd() | |
s = comp(''' | |
100> | |
0+<+6< 0+<+6< 0+<+6< 7< 1+<+6< 2+<+6< 6+<+6< 7< 4+<+6< 5+<+6< 2+<+ | |
''') + mult() + '@' + putd() | |
s = comp(''' | |
60> | |
8+<+6< 2+<+6< 3+<+ @ | |
''') + div6() + '@' + putd() | |
s = comp(''' | |
60> | |
8+<+6< 2+<+6< 0+<+ @ | |
''') + hosuu() + '@' + putd() | |
np = 0 | |
def movp(p): | |
global np | |
dp = p - np | |
#print dp,p,np | |
np = p | |
if dp > 0: | |
return comp(''' | |
<%d+ | |
[ | |
[>[7>]7><+>7<7<[7<]7><-] | |
>[7>]7><- | |
] | |
> | |
''' % dp) | |
elif dp < 0: | |
return comp(''' | |
<%d+ | |
[ | |
[>7<7<[7<]7><+>[7>]7><-] | |
>7<7<[7<]7><- | |
] | |
> | |
''' % (-dp)) | |
else: | |
return '' | |
# stp n ls h | |
def init(n,ls): | |
global np | |
np = 0 | |
return '>>>>>' + comp('+>' + str(n) + '+' + ''' | |
[ | |
>%d+ | |
[ | |
>+ | |
<[7>+7<-]7>- | |
#lsを移す | |
] | |
>7<[7<] | |
7><< | |
[ | |
>>[7>]7><<+>>7<7<[7<]7><< | |
- | |
] | |
#nを移す | |
>>[7>]7><<- | |
#n-1 | |
] | |
<-[+7<-]>>> | |
''' % ls) | |
def readd(): | |
return comp('>7>,48- 7<,48- >,[-]<<') | |
def move(p,q): | |
if p==q: | |
return '' | |
# now q | |
if p < q: | |
s = comp(''' | |
<<7<-[+7<-]+>> | |
# at p [r _ h' x | |
[ | |
>[ | |
#r _ h x' | |
<<<7>-[+7>-]+>>>+ | |
<<<7<-[+7<-]+>>>- | |
] | |
<<<7>-[+7>-]7>+ | |
#qをずらす | |
7<-[+7<-]7>+ | |
#pをずらす | |
>> | |
] | |
<<- | |
#pを消す | |
-[+7>-] | |
#qを消す | |
>>7<[7<]7> | |
''') | |
elif p > q: | |
s = comp(''' | |
<<7>-[+7>-]+>> | |
# at p [r _ h' x | |
[ | |
>[ | |
#r _ h x' | |
<<<7<-[+7<-]+>>>+ | |
<<<7>-[+7>-]+>>>- | |
] | |
<<<7<-[+7<-]7>+ | |
#qをずらす | |
7>-[+7>-]7>+ | |
#pをずらす | |
>> | |
] | |
<<- | |
#pを消す | |
-[+7<-] | |
#qを消す | |
>>7<[7<]7> | |
''') | |
return ( | |
movp(p) + '<<+>>' + | |
movp(q) + '<<+>>' + | |
s | |
) | |
def move_copy(p,q,r): | |
if p==q: | |
q,r = r,q | |
if q==p: | |
return '' | |
return ( | |
movp(p) + | |
comp(''' | |
[> [>+>+<<-] >[<+>-] <<7>] | |
# h x 0 x | |
7<[7<]7> | |
''') + move(p,q) + | |
movp(p) + | |
comp(''' | |
[>>> [<<+>>-] <<<7>] | |
# h x | |
7<[7<]7> | |
''') + move(p,r) | |
) | |
#s = init(3,5) + '@' + movp(2) + '@' + movp(1) + '@' | |
#s = init(3,5) + readd() + movp(1) + readd() + movp(0) + add() + movp(1) + putd() | |
#s = init(3,5) + readd() + putd() | |
#s = init(3,5) + movp(1) + readd() + '@' + move_copy(1,0,2) + '@' | |
s = init(40,6) | |
for i in range(12): | |
s += ( | |
movp(i*3) + readd() | |
) | |
for i in range(3): | |
s += ( | |
movp(i*3) + hosuu() | |
) | |
for j in range(0,3): | |
t = j*3+i+3 | |
print i,t | |
s += ( | |
(move_copy(i*3,i*3,t*3+1) if j!=2 else move(i*3,t*3+1)) + | |
movp(t*3) + add() | |
) | |
def movc(p,q): | |
return move_copy(p,p,q) | |
s += ( | |
movc(19,33) + | |
movc(34,21) + | |
movc(22,27) + | |
movc(28,24) + | |
movc(25,30) + | |
movc(31,18) | |
) | |
s += '@' | |
for i in range(6,12): | |
s += movp(i*3) + mult() | |
for i in range(3): | |
s += move(i*3+10,i*3+9) | |
s += '@' | |
s += ( | |
move(23,10) + | |
move(26,13) + | |
move(20,16) + | |
move(32,11) + | |
move(35,14) + | |
move(29,17) | |
) | |
s += '@' | |
for i in range(3): | |
s += ( | |
movp(i*3+11) + hosuu() + | |
movp(i*3+10) + add() + | |
move(i*3+11,i*3+10) + | |
movp(i*3+9) + mult() | |
) | |
s += '@' | |
s += ( | |
move(11,3) + move(14,4) + move(17,5) + | |
movp(3) + add() + movp(4) + add() + movp(5) + '@' + div6() | |
) | |
s += '@' | |
s += putd() | |
""" | |
#s += putd() | |
for i in range(0,12): | |
for j in range(3): | |
s += ( | |
movp(i*3+j) + putd() + comp('[-]32+.[-]7>') | |
) | |
s += comp('<[-]10+.>') | |
""" | |
#s = init(39,6) + movp(0) + readd() + putd() + '[-]+10.[-]7>' + movp(1) + readd() + putd() + '[-]+10.[-]7>' | |
#s = init(39,6) + movp(0) + readd() + putd() + comp('[-]10+.[-]7>') | |
def shlink(s): | |
ts = "" | |
for c in s: | |
ts += c | |
if ts[-2:]=='<>': | |
ts = ts[:-2] | |
elif ts[-2:]=='><': | |
ts = ts[:-2] | |
return ts | |
#s = init(4,6) + movp(0) + readd() + movp(1) + readd() + movp(0) + '@' + mult() | |
s = shlink(s) | |
r.clipboard_append(s) | |
#print s | |
print len(s) | |
v = '29 84 73 51 22 95 03 02 69 95 01 17 ' | |
for k in range(1): | |
bf_interpreter.eval(s,v) | |
v = v[9:] + v[:9] | |
#bf_interpreter.eval(s,'98 76 ') | |
#bf_interpreter.eval(s,'00 00 00 03 05 06 01 02 04 09 07 08 ') | |
""" | |
s = [[],[],[]] | |
s[2] = [0 for _ in range(3)] | |
s[1] = [1,2,3] | |
s[0] = [4,5,6] | |
p = 0 | |
rp = 0 | |
while p < len(s[0]): | |
r = rp | |
q = 0 | |
while r < len(s[2]): | |
#s[2][r] += s[1][q] * s[0][p] | |
#copy(s[0][p]) | |
m = s[0][p] | |
while s[0][p]>0: | |
#copy(s[1][q]) | |
n = s[1][q] | |
while s[1][q] >0: | |
s[1][q] -= 1 | |
s[2][r] += 1 | |
s[1][q] = n | |
s[0][p] -= 1 | |
s[0][p] = m | |
r += 1 | |
q += 1 | |
p += 1 | |
rp += 1 | |
print s[2] | |
""" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment