|
|
|
@ -9,7 +9,7 @@ function parse( s ){
|
|
|
|
|
else if(s~/^\]/){ # ]
|
|
|
|
|
l = sl[slp--]
|
|
|
|
|
r = pindex
|
|
|
|
|
print "left",l,"right",r
|
|
|
|
|
# print "left",l,"right",r
|
|
|
|
|
num[++pindex] = -1
|
|
|
|
|
ile[pindex] = l
|
|
|
|
|
iri[pindex] = r
|
|
|
|
@ -22,6 +22,13 @@ function parse( s ){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanparse(s){
|
|
|
|
|
delete ile; delete iri; delete num; delete sl
|
|
|
|
|
slp = 0
|
|
|
|
|
pindex = 0
|
|
|
|
|
parse(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parent(id){
|
|
|
|
|
for(i in ile)
|
|
|
|
|
if(ile[i]==id || iri[i]==id) return i
|
|
|
|
@ -50,14 +57,14 @@ function treetostring(id){
|
|
|
|
|
function leftitemi(id){
|
|
|
|
|
do{
|
|
|
|
|
id--
|
|
|
|
|
}while(num[id]<0 && id>0)
|
|
|
|
|
}while((num[id]<0 && id>0))
|
|
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function rightitemi(id){
|
|
|
|
|
do{
|
|
|
|
|
id++
|
|
|
|
|
}while(num[id]<0 && id<=length(num))
|
|
|
|
|
}while((num[id]<0 && id<=length(num)))
|
|
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -79,8 +86,8 @@ function explode(id){
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toexplode(){
|
|
|
|
|
for(id=1;id<=length(num);id++){
|
|
|
|
|
if((num[id]>0) && (levels(id)>4)) return id
|
|
|
|
|
for(id=1;id<=pindex;id++){
|
|
|
|
|
if((num[id]>=0) && (levels(id)>4) && num[parent(id)]<0) return id
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
@ -97,23 +104,63 @@ function dosplit(id){
|
|
|
|
|
}
|
|
|
|
|
function tosplit(){
|
|
|
|
|
for(id=1;id<=pindex;id++){
|
|
|
|
|
print id, (num[id]-10>0), num[id]
|
|
|
|
|
if((num[id]-10>0)) return id
|
|
|
|
|
# print id, (num[id]-10>0), num[id]
|
|
|
|
|
if((num[id]-10>=0) && num[parent(id)]<0 ) return id
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reduce(){
|
|
|
|
|
do{
|
|
|
|
|
ind = toexplode()
|
|
|
|
|
print "explode?", ind, num[ind], parent(ind), num[parent(ind)],num[leftitemi(ind)]
|
|
|
|
|
|
|
|
|
|
if(ind>0){
|
|
|
|
|
explode(ind)
|
|
|
|
|
print "exploded", treetostring(root)
|
|
|
|
|
s = treetostring(root)
|
|
|
|
|
cleanparse(s)
|
|
|
|
|
# print "to split?",tosplit()
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
ind = tosplit()
|
|
|
|
|
print "split?", ind, num[ind], parent(ind), num[parent(ind)], ile[parent(ind)], iri[parent(ind)]
|
|
|
|
|
if(ind>0){
|
|
|
|
|
dosplit(ind)
|
|
|
|
|
print "split", treetostring(root)
|
|
|
|
|
s = treetostring(root)
|
|
|
|
|
cleanparse(s)
|
|
|
|
|
}
|
|
|
|
|
print toexplode(), tosplit()
|
|
|
|
|
} while( ((toexplode()>0) || (tosplit()>0)) )
|
|
|
|
|
print "done"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function magnitude(id){
|
|
|
|
|
if(num[id]>=0) return num[id] # regular number
|
|
|
|
|
# node
|
|
|
|
|
return magnitude(ile[id])*3 + magnitude(iri[id])*2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BEGIN{ pindex = 0 }
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
NR==1{
|
|
|
|
|
parse($0)
|
|
|
|
|
proot = root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NR>1{
|
|
|
|
|
parse( $0 )
|
|
|
|
|
if(NR>1){
|
|
|
|
|
num[++pindex] = -1
|
|
|
|
|
ile[pindex] = proot
|
|
|
|
|
iri[pindex] = root
|
|
|
|
|
root = pindex
|
|
|
|
|
}
|
|
|
|
|
num[++pindex] = -1
|
|
|
|
|
ile[pindex] = proot
|
|
|
|
|
iri[pindex] = root
|
|
|
|
|
root = pindex
|
|
|
|
|
print "raw add",NR, treetostring(root)
|
|
|
|
|
reduce()
|
|
|
|
|
s = treetostring(root)
|
|
|
|
|
cleanparse(s)
|
|
|
|
|
proot = root
|
|
|
|
|
print "after",NR,treetostring(root)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
END{
|
|
|
|
@ -128,27 +175,14 @@ END{
|
|
|
|
|
|
|
|
|
|
# print num[1], "is at",levels(1),"levels"
|
|
|
|
|
|
|
|
|
|
s = treetostring(root)
|
|
|
|
|
cleanparse(s)
|
|
|
|
|
print treetostring(root)
|
|
|
|
|
|
|
|
|
|
c = 0;
|
|
|
|
|
do{
|
|
|
|
|
ind = toexplode()
|
|
|
|
|
print "explode?", ind
|
|
|
|
|
reduce()
|
|
|
|
|
|
|
|
|
|
if(ind>0){
|
|
|
|
|
explode(ind)
|
|
|
|
|
print "exploded"
|
|
|
|
|
}
|
|
|
|
|
ind = tosplit()
|
|
|
|
|
print "split?", ind
|
|
|
|
|
if(ind>0){
|
|
|
|
|
dosplit(ind)
|
|
|
|
|
print "split"
|
|
|
|
|
}
|
|
|
|
|
print treetostring(root)
|
|
|
|
|
c++
|
|
|
|
|
print toexplode(), tosplit(), c
|
|
|
|
|
} while( ((toexplode()>0) || (tosplit()>0)) )
|
|
|
|
|
print "done"
|
|
|
|
|
print treetostring(root)
|
|
|
|
|
|
|
|
|
|
print magnitude(root)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|