diff --git a/18/0 b/18/0 deleted file mode 100644 index 7ed6ff8..0000000 --- a/18/0 +++ /dev/null @@ -1 +0,0 @@ -5 diff --git a/18/18.awk b/18/18.awk index 9186a2b..54e1dc8 100644 --- a/18/18.awk +++ b/18/18.awk @@ -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,25 +104,65 @@ 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 } -{ - parse( $0 ) - if(NR>1){ - num[++pindex] = -1 - ile[pindex] = proot - iri[pindex] = root - root = pindex - } +NR==1{ + parse($0) proot = root } +NR>1{ + parse( $0 ) + 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{ OFS=":" print pindex, root @@ -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) } diff --git a/18/6 b/18/6 deleted file mode 100644 index 3f10ffe..0000000 --- a/18/6 +++ /dev/null @@ -1 +0,0 @@ -15 \ No newline at end of file