Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local getc=function(str,pos) return str:sub(pos,pos) end
- local validchars={["("]=true,[")"]=true,}
- local skipinvalid=function(str,start)
- local pos=start
- while not validchars[getc(str,pos)] and pos<=#str do
- pos=pos+1
- end
- return pos
- end
- local read read=function(input,start)
- local data={}
- local d
- local pos=start
- while pos<=#input do
- pos=skipinvalid(input,pos)
- local char=getc(input,pos)
- --print(char)
- if char=="(" then
- pos,d=read(input,pos+1)
- table.insert(data,d)
- elseif char==")" then
- return pos+1,data
- end
- end
- --table.remove(data)
- --error("unbalanced brackets")
- return pos+1,data
- end
- local parse=function(str)
- local _,data=read(str,1)
- return data
- end
- local output output=function(data)
- io.write("(")
- for i,v in ipairs(data) do
- output(v)
- end
- io.write(")")
- end
- local make_output=function(data)
- for i,v in ipairs(data) do
- output(v)
- end
- end
- local is_Empty=function(data)
- return #data==0
- end
- local copy copy=function(data)
- local new={}
- for i,v in ipairs(data) do
- new[i]=copy(v)
- end
- return new
- end
- local equals equals=function(a,b)
- local eq=#a==#b
- for i=1,#a do
- if not eq then break end
- eq=eq and equals(a[i],b[i])
- end
- return eq
- end
- local substitute substitute=function(X,Y,Z)
- --If X is Y, replace it with Z
- if equals(X,Y) then
- --print("R1")
- return copy(Z)
- --If X is empty, leave it untouched
- elseif #X==0 then
- --print("R2")
- return X
- --If X has exactly one element, then substutute Y with Z in that element
- elseif #X==1 then
- --print("R3")
- X[1]=substitute(X[1],Y,Z)
- return X
- else
- --If the first element of X is Y, leave it untouched
- -- interpreted as "if the first element of X equals Y, then leave the first element of X untouched"
- local newX={}
- if not equals(X[1],Y) then
- -- print("R4")
- newX[1]=substitute(X[1],Y,Z)
- else
- newX[1]=copy(X[1])
- end
- --In each element of X starting from the second element, substutute Y with Z
- --print("R5")
- for i=2,#X do
- newX[i]=substitute(X[i],Y,Z)
- end
- return newX
- end
- error("asdasd")
- end
- local applyRules=function(data)-->halt, rules matched
- --If there are no global groups, then halt the program.
- local count=#data
- if count==0 then
- --make_output(data)
- return true,true
- --If the first global group is empty and it is the only global group, then halt the program.
- elseif count==1 and is_Empty(data[1]) then
- --make_output(data)
- return true,true
- --If the first global group is empty and the second global group is also empty, then remove one of them.
- elseif count>=2 and is_Empty(data[1]) and is_Empty(data[2]) then
- table.remove(data,math.random(1,2))
- return false,true
- --If the first global group is empty and the second global group is non-empty, then swap them.
- elseif count>=2 and is_Empty(data[1]) and (not is_Empty(data[2])) then
- data[1],data[2]=data[2],data[1]
- return false,true
- --If the first global group has exactly one element, then replace the first global group with the content of that element (so, fo example, ((()())) will be replaced with ()()).
- elseif count>=1 and not is_Empty(data[1]) and #data[1]==1 then
- data[1]=data[1][1]
- return false,true
- --If the first global group has two or more elements and it is the only global group, then halt the program.
- elseif count==1 and #data[1]>1 then
- --make_output(data)
- return true,true
- end
- return false,false
- end
- local compute=function(data)
- --Let's call the first one A and the second one B.
- local A=copy(data[1])
- local B=copy(data[2])
- --First, we remove B:
- table.remove(data,2)
- --remove A from global as we will insert it's innards later
- --We first select the first element of A (let's call it C and color it red):
- local C=copy(A[1])
- --Now, we remove C and
- table.remove(A,1)
- --then we remove parentheses of A:
- --this is done when substituting
- -- Finally, for each remaining element of A (the groups that are left green after removing C and parentheses of A), we recursively substitute C with B. How substitution works is explained in the next paragraph.
- table.remove(data,1)--remove a to make room to a's innards
- for i=1,#A do
- local innards=substitute(A[i],C,B)
- table.insert(data,i,innards)
- end
- end
- local make_random=function(count)
- local toreturn={"(",")"}
- for i=1,count do
- local pivot=math.random(1,#toreturn+1)
- table.insert(toreturn,pivot,"(")
- pivot2=math.random(pivot+1,#toreturn+1)
- table.insert(toreturn,pivot2,")")
- end
- return table.concat(toreturn)
- end
- --replace io.read("*a") with the source code surrounded with [[]] to embed code
- local input=io.read("*a")
- local halt=false
- local matchrule=true
- --load data
- local data=parse(input)
- -- do the actual computation
- -- all the setup for this line
- while not halt do
- halt,matchrule=applyRules(data)
- if not matchrule then
- compute(data)
- end
- --uncomment the next to lines to see how the state evolves
- -- make_output(data)
- -- print("")
- end
- --print("\nFINAL output")
- make_output(data)
- io.write("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement