From sepp2k solution - 148
eval"a=[i=0]*3e4;"+$<.bytes.map{|b|{?.,"putc a[i]",?,,"a[i]=getc",?[,"while a[i]>0",?],"end",?<,"i-=1",?>,"i+=1",?+,"a[i]+=1",?-,"a[i]-=1"}[b]}*";"
eval"a=[i=0]*3e4;"+$<.bytes.map{ can be replaced with a=[i=0]*3e4;eval$<.bytes.map{ -3 bytes
*";" => *$/ -1 bytes
"while a[i]>0" and"end" => "(" and ")while(a[i]>0)" -1 bytes
And we get 143 (5 bytes less)
a=[i=0]*3e4;eval$<.bytes.map{|b|{?.,"putc a[i]",?,,"a[i]=getc",?[,"(",?],")while a[i]>0",?<,"i-=1",?>,"i+=1",?+,"a[i]+=1",?-,"a[i]-=1"}[b]}*$/
And what if there aren't any comments in input (only +-<>[],.) http://codepad.org/EihHsoJO
we can write like this:
a=[i=0]*3e4;eval$<.bytes.map{|b|%w{putc(a[i]) a[i]=getc ( )while(a[i]>0) i-=1 i+=1 a[i]+=1 a[i]-=1}[".,[]<>+-\n".index b]}*$/
And this is 126 bytes, if there wouldn't be "\n" at the end, we can skip it in this part ".,[]<>+-\n" => ".,[]<>+-" saving 2 bytes
And this can be shorten to:
a=[i=0]*3e4;eval$<.bytes.map{|b|%w{i-=1 ( i+=1 )while(0<a[i]) a[i]+=1 a[i]=getc a[i]-=1 putc(a[i])}[b%30%9]}*$/
which is 112 bytes
where b%30%9 is a mapping from ascii code to array index
How to find this formula?
Very easy:
c="<[>]+,-."
(1..99).each do |i|
(1..99).each do |j|
r = c.each_byte.map {|a| a%i%j}.select {|x| x < c.size}.uniq
puts "#{r} #{i} #{j} " if r.size==c.size
end
end
>>>
[0, 1, 2, 3, 4, 5, 6, 7] 30 9
[4, 5, 6, 7, 0, 1, 2, 3] 43 13
[4, 3, 6, 5, 7, 0, 1, 2] 44 12
[0, 7, 2, 1, 3, 4, 5, 6] 52 8
[0, 7, 2, 1, 3, 4, 5, 6] 60 8
So if only we can assume, that there would be only <>+-[],. whe can shorten the solution to 112 bytes
,.-+[]<>is a comment in brainfuck. (I'm asking because it makes my implementation quite a bit longer than it needs to be) – sepp2k Jan 30 '11 at 15:33