2021-12-19 04:34:22 +00:00
|
|
|
function parse( s ){
|
|
|
|
delete sl; delete sr
|
|
|
|
while(length(s)>0){
|
|
|
|
if(match(s,/^[0-9]+/)){
|
|
|
|
num[++pindex] = substr(s,RSTART,RLENGTH)
|
|
|
|
s = substr(s,RSTART+RLENGTH)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
else if(s~/^\]/){ # ]
|
|
|
|
l = sl[slp--]
|
|
|
|
r = pindex
|
2021-12-20 04:12:14 +00:00
|
|
|
# print "left",l,"right",r
|
2021-12-19 04:34:22 +00:00
|
|
|
num[++pindex] = -1
|
|
|
|
ile[pindex] = l
|
|
|
|
iri[pindex] = r
|
|
|
|
if(slp==0) root = pindex
|
|
|
|
}
|
|
|
|
else if(s~/^,/){ # ,
|
|
|
|
sl[++slp] = pindex
|
|
|
|
}
|
|
|
|
s = substr(s,2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 04:12:14 +00:00
|
|
|
function cleanparse(s){
|
|
|
|
delete ile; delete iri; delete num; delete sl
|
|
|
|
slp = 0
|
|
|
|
pindex = 0
|
|
|
|
parse(s)
|
|
|
|
}
|
|
|
|
|
2021-12-19 04:34:22 +00:00
|
|
|
function parent(id){
|
|
|
|
for(i in ile)
|
|
|
|
if(ile[i]==id || iri[i]==id) return i
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
function levels(id){
|
|
|
|
i = id
|
|
|
|
count = 0
|
|
|
|
if(id==root) return 0
|
|
|
|
do{
|
|
|
|
i = parent(i)
|
|
|
|
if(i<0) return -1
|
|
|
|
count++
|
|
|
|
}while(i!=root)
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
function treetostring(id){
|
|
|
|
if(num[id]<0)
|
|
|
|
return "[" treetostring(ile[id]) "," treetostring(iri[id]) "]"
|
|
|
|
else
|
|
|
|
return num[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
function leftitemi(id){
|
|
|
|
do{
|
|
|
|
id--
|
2021-12-20 04:12:14 +00:00
|
|
|
}while((num[id]<0 && id>0))
|
2021-12-19 04:34:22 +00:00
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
function rightitemi(id){
|
|
|
|
do{
|
|
|
|
id++
|
2021-12-20 04:12:14 +00:00
|
|
|
}while((num[id]<0 && id<=length(num)))
|
2021-12-19 04:34:22 +00:00
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function explode(id){
|
|
|
|
lefti = leftitemi(id)
|
|
|
|
if(lefti>0){
|
|
|
|
num[lefti] += num[id]
|
|
|
|
num[id]=-1
|
|
|
|
}
|
|
|
|
righti = rightitemi(id)
|
|
|
|
rightrighti = rightitemi(righti)
|
|
|
|
if(rightrighti<=length(num)){
|
|
|
|
num[rightrighti] += num[righti]
|
|
|
|
num[righti] = -1
|
|
|
|
}
|
|
|
|
p = parent(id)
|
|
|
|
num[p] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function toexplode(){
|
2021-12-20 04:12:14 +00:00
|
|
|
for(id=1;id<=pindex;id++){
|
|
|
|
if((num[id]>=0) && (levels(id)>4) && num[parent(id)]<0) return id
|
2021-12-19 04:34:22 +00:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
function dosplit(id){
|
|
|
|
n = num[id]
|
|
|
|
nl = int(n/2)
|
|
|
|
nr = int(n/2) + n%2
|
|
|
|
num[id] = -1
|
|
|
|
num[++pindex] = nl
|
|
|
|
ile[id] = pindex
|
|
|
|
num[++pindex] = nr
|
|
|
|
iri[id] = pindex
|
|
|
|
}
|
|
|
|
function tosplit(){
|
|
|
|
for(id=1;id<=pindex;id++){
|
2021-12-20 04:12:14 +00:00
|
|
|
# print id, (num[id]-10>0), num[id]
|
|
|
|
if((num[id]-10>=0) && num[parent(id)]<0 ) return id
|
2021-12-19 04:34:22 +00:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2021-12-20 04:12:14 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-19 04:34:22 +00:00
|
|
|
BEGIN{ pindex = 0 }
|
|
|
|
|
2021-12-20 04:12:14 +00:00
|
|
|
NR==1{
|
|
|
|
parse($0)
|
|
|
|
proot = root
|
|
|
|
}
|
|
|
|
|
|
|
|
NR>1{
|
2021-12-19 04:34:22 +00:00
|
|
|
parse( $0 )
|
2021-12-20 04:12:14 +00:00
|
|
|
num[++pindex] = -1
|
|
|
|
ile[pindex] = proot
|
|
|
|
iri[pindex] = root
|
|
|
|
root = pindex
|
|
|
|
print "raw add",NR, treetostring(root)
|
|
|
|
reduce()
|
|
|
|
s = treetostring(root)
|
|
|
|
cleanparse(s)
|
2021-12-19 04:34:22 +00:00
|
|
|
proot = root
|
2021-12-20 04:12:14 +00:00
|
|
|
print "after",NR,treetostring(root)
|
2021-12-19 04:34:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
END{
|
|
|
|
OFS=":"
|
|
|
|
print pindex, root
|
|
|
|
# for(i in num)
|
|
|
|
# print i,num[i],levels(i)
|
|
|
|
|
|
|
|
# print "l,r, root:", root
|
|
|
|
# for(i in ile)
|
|
|
|
# print i, ile[i], iri[i]
|
|
|
|
|
|
|
|
# print num[1], "is at",levels(1),"levels"
|
|
|
|
|
2021-12-20 04:12:14 +00:00
|
|
|
s = treetostring(root)
|
|
|
|
cleanparse(s)
|
2021-12-19 04:34:22 +00:00
|
|
|
print treetostring(root)
|
|
|
|
|
2021-12-20 04:12:14 +00:00
|
|
|
reduce()
|
2021-12-19 04:34:22 +00:00
|
|
|
|
2021-12-20 04:12:14 +00:00
|
|
|
print treetostring(root)
|
|
|
|
|
|
|
|
print magnitude(root)
|
2021-12-19 04:34:22 +00:00
|
|
|
|
|
|
|
}
|