Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Copyright 2008 Tritonio
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- --]]
- rep=string.rep
- function read(n)
- return seek(n,k,1,0,1)..
- rep(">",k)..
- rep("<"..rep(">",2*k+2*n)..
- "[-"..rep("<",n+k).."+"..rep("<",n+k).."+"..rep(">",2*n+2*k).."]"..
- rep("<",2*k+2*n).."[-"..rep(">",2*k+2*n).."+"..rep("<",2*k+2*n)..
- "]",k)..
- retur(n,k,1,0,1)
- end
- function left(n,k,w)
- return rep(">",n+k-1)..
- rep(">"..rep("[-"..rep("<",k).."+"..rep(">",k).."]",1-w),k)..
- rep(">[-"..rep("<",k).."+"..rep(">",k).."]",n)..
- rep("<",2*k+2*n-1)..
- rep("<[-"..rep(">",2*k+2*n).."+"..rep("<",2*k+2*n).."]",k)
- end
- function retur(n,k,i,w,f)
- local ret= rep(">",2*k+2*n-i).."[-"..rep("<",2*k+2*n-i)..
- left(n,k,w)..rep(">",2*k+2*n-i)
- if i>1 then ret=ret..
- rep(">-<"..rep("<",2*k+2*n-i)..
- retur(n,k,i-1,w,0)..
- rep(">",2*k+2*n-i),(i>1) and 1 or 0) end
- ret=ret.."]"..rep("<",2*k+2*n-i) --simplirosa kai mia ].
- if i<n and f==1 then ret=ret..rep(retur(n,k,i+1,w,1),(i<n and 1 or 0)*f) end
- return ret
- end
- function seek(n,k,i,w,f)
- local ret= rep(">",n+k-i).."[-"..rep(rep(">",n+k).."+"..rep("<",n+k),f)..
- rep("<",n+k-i)..right(n,k,w)..
- rep(">",n+k-i) --diakopi apotoma se sxesi me to xarti
- if i>1 then ret=ret..rep(">-<"..rep("<",n+k-i)..
- seek(n,k,i-1,w,0)..
- rep(">",n+k-i),(i>1) and 1 or 0) end
- --apotomi sinexia
- ret=ret.."]"..rep("<",n+k-i)
- if i<n and f==1 then ret=ret..rep(seek(n,k,i+1,w,1),(i<n and 1 or 0)*f) end
- return ret
- end
- function write(n,k,i,w,f)
- return seek(n,k,1,1,1)..
- rep(">",3*k+2*n)..
- rep("<[-]",k)..rep("<",n)..
- rep("<[-"..rep(">",n+k).."+"..rep("<",n+k).."]",k)..
- rep("<",n+k)..
- retur(n,k,1,1,1)
- end
- function right(n,k,w)
- return rep(">",3*k+2*n)..
- rep("<[-"..rep("<",2*k+2*n).."+"..rep(">",2*k+2*n).."]",k)..
- rep("<[-"..rep(">",k).."+"..rep("<",k).."]",n)..
- rep("<"..rep("[-"..rep(">",k).."+"..rep("<",k).."]",w),k)..
- rep("<[-"..rep(">",k).."+"..rep("<",k).."]",n)
- end
- function optimize(code)
- repeat
- local changes=0
- local tempchanges=0
- code,tempchanges=string.gsub(code,"<>","")
- changes=tempchanges+changes
- code,tempchanges=string.gsub(code,"><","")
- changes=tempchanges+changes
- code,tempchanges=string.gsub(code,"%-%+","")
- changes=tempchanges+changes
- code,tempchanges=string.gsub(code,"%+%-","")
- changes=tempchanges+changes
- --print(changes)
- until changes==0
- return code
- end
- print("Brainfuck Array Wizard by Tritonio")
- print()
- io.write("How many bytes per cell? ")
- k=io.read()/1
- io.write("How many cells? ")
- cells=io.read()
- n=math.ceil(math.log(cells)/math.log(256))
- if n==0 then n=1 end
- print()
- print("This is the structure of the table: "..rep("0",k)..rep("I",n)..rep("D",k)..rep("0",n).."("..cells*k.." empty cells for array data)") --to n*k den einai sosto.
- print("(0 stands for empty cell, I for index, D for data)")
- print("(The most significant index is the first one)")
- print()
- print("The total size of the table is "..cells*k+k+n+k+n..".")
- print("The overhead because of the indexes etc is "..math.floor(0.5+100000*(k+n+k+n)/(cells*k+k+n+k+n))/1000 .."% of the total size.")
- print()
- print("To write to the table fill in the I (index) and the D (data) positions in the header, move the Brainfuck pointer to the beginning of the structure and execute the following code:")
- print(optimize(write(n,k)))
- print("After the execution, the indexes and the data fields will be zeroed and the Brainfuck pointer will be left at the beginning of the structure.")
- --print(write(n,k))
- print()
- print("To read from the table fill the I (index) fields, move the Brainfuck pointer to the beginning of the structure and execute the following code:")
- print(optimize(read(n,k)))
- --print(read(n,k))
- print("After the execution, the indexes will be zeroed, the Brainfuck pointer will be left at the beginning of the structure and the data fields will contain the read data.")
- io.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement