From: Simon Strandgaard Date: 2006-08-10T05:39:17+09:00 Subject: Re: Codegolf - Writing a Brainf*ck interpreter On 8/9/06, Daniel Martin wrote: > "Simon Strandgaard" writes: > > well here is my 141 bytes long version :-) [snip] > Note that both this and the earlier one you posted don't conform to > the interface of the codegolf challenge: namely, that bf program will > be presented on stdin, and separated from input by a !. aha.. I did'nt really use the rules from codegolf. I looked it up at wikipedia. I should had seen this. > A correct bf interpreter using the traditional 8-bit cell should > output nothing. Yours... doesn't. True, the codegolf people didn't > say you had to use an 8-bit cell, so maybe this is ok. Ok, I have to rethink the impl to restrict it to 8 bit. Now I see why people is using strings for this. > However, I hadn't known about putc, and seeing your code gives me some > ideas. > > Note that even with your version, you can save two bytes by NOT doing > the .gsub bit, and expanding X into d[a]. Doing that substitution may > save you 15 characters out of the %w block, but > .gsub(/X/,'d[i]') > is seventeen characters long. Argh.. I misscalculated.. 5 times d[i] = 20 bytes 5 times X + 17 = 22 bytes Thanks for your hints. > Anyway, here's where I'm at, at 168 characters: > > j='d="\0"*8**5;a=0' > while i=STDIN.getc > j+=' > '+%w{1 a-=1 a+=1 while(d[a]>0) > end putc(d[a]) d[a]=STDIN.getc||exit > d[a]+=1 d[a]-=1}[" > <>[].,+-".index(i)||break] > end > eval j Idea for improvement: while(d[a]>0) end #=> 13 bytes ( )while(d[a]>0) #=> 12 bytes -- Simon Strandgaard