From 275925c251859505b311b6bf29002100e70425b5 Mon Sep 17 00:00:00 2001 From: sejo Date: Sat, 18 Dec 2021 22:34:22 -0600 Subject: [PATCH] day 18, almost done --- 18/0 | 1 + 18/18.awk | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 18/6 | 1 + 18/Makefile | 14 +++++ 4 files changed, 170 insertions(+) create mode 100644 18/0 create mode 100644 18/18.awk create mode 100644 18/6 create mode 100644 18/Makefile diff --git a/18/0 b/18/0 new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/18/0 @@ -0,0 +1 @@ +5 diff --git a/18/18.awk b/18/18.awk new file mode 100644 index 0000000..9186a2b --- /dev/null +++ b/18/18.awk @@ -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" + +} diff --git a/18/6 b/18/6 new file mode 100644 index 0000000..3f10ffe --- /dev/null +++ b/18/6 @@ -0,0 +1 @@ +15 \ No newline at end of file diff --git a/18/Makefile b/18/Makefile new file mode 100644 index 0000000..4d1a823 --- /dev/null +++ b/18/Makefile @@ -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