day 18, almost done
parent
b5bfb20e9b
commit
275925c251
@ -0,0 +1,154 @@
|
||||
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
|
||||
print "left",l,"right",r
|
||||
num[++pindex] = -1
|
||||
ile[pindex] = l
|
||||
iri[pindex] = r
|
||||
if(slp==0) root = pindex
|
||||
}
|
||||
else if(s~/^,/){ # ,
|
||||
sl[++slp] = pindex
|
||||
}
|
||||
s = substr(s,2)
|
||||
}
|
||||
}
|
||||
|
||||
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--
|
||||
}while(num[id]<0 && id>0)
|
||||
return id
|
||||
}
|
||||
|
||||
function rightitemi(id){
|
||||
do{
|
||||
id++
|
||||
}while(num[id]<0 && id<=length(num))
|
||||
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(){
|
||||
for(id=1;id<=length(num);id++){
|
||||
if((num[id]>0) && (levels(id)>4)) return id
|
||||
}
|
||||
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++){
|
||||
print id, (num[id]-10>0), num[id]
|
||||
if((num[id]-10>0)) return id
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
BEGIN{ pindex = 0 }
|
||||
|
||||
{
|
||||
parse( $0 )
|
||||
if(NR>1){
|
||||
num[++pindex] = -1
|
||||
ile[pindex] = proot
|
||||
iri[pindex] = root
|
||||
root = pindex
|
||||
}
|
||||
proot = root
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
print treetostring(root)
|
||||
|
||||
c = 0;
|
||||
do{
|
||||
ind = toexplode()
|
||||
print "explode?", ind
|
||||
|
||||
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"
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
1t:
|
||||
awk -f 18.awk test
|
||||
|
||||
1i:
|
||||
awk -f 18.awk input
|
||||
|
||||
2t:
|
||||
awk -f 18_2.awk test
|
||||
|
||||
2i:
|
||||
awk -f 18_2.awk input
|
||||
|
||||
paste:
|
||||
xclip -sel c -o > test
|
Loading…
Reference in New Issue