day 18, pt 1!
This commit is contained in:
parent
275925c251
commit
001a8043ef
102
18/18.awk
102
18/18.awk
|
@ -9,7 +9,7 @@ function parse( s ){
|
||||||
else if(s~/^\]/){ # ]
|
else if(s~/^\]/){ # ]
|
||||||
l = sl[slp--]
|
l = sl[slp--]
|
||||||
r = pindex
|
r = pindex
|
||||||
print "left",l,"right",r
|
# print "left",l,"right",r
|
||||||
num[++pindex] = -1
|
num[++pindex] = -1
|
||||||
ile[pindex] = l
|
ile[pindex] = l
|
||||||
iri[pindex] = r
|
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){
|
function parent(id){
|
||||||
for(i in ile)
|
for(i in ile)
|
||||||
if(ile[i]==id || iri[i]==id) return i
|
if(ile[i]==id || iri[i]==id) return i
|
||||||
|
@ -50,14 +57,14 @@ function treetostring(id){
|
||||||
function leftitemi(id){
|
function leftitemi(id){
|
||||||
do{
|
do{
|
||||||
id--
|
id--
|
||||||
}while(num[id]<0 && id>0)
|
}while((num[id]<0 && id>0))
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
function rightitemi(id){
|
function rightitemi(id){
|
||||||
do{
|
do{
|
||||||
id++
|
id++
|
||||||
}while(num[id]<0 && id<=length(num))
|
}while((num[id]<0 && id<=length(num)))
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +86,8 @@ function explode(id){
|
||||||
}
|
}
|
||||||
|
|
||||||
function toexplode(){
|
function toexplode(){
|
||||||
for(id=1;id<=length(num);id++){
|
for(id=1;id<=pindex;id++){
|
||||||
if((num[id]>0) && (levels(id)>4)) return id
|
if((num[id]>=0) && (levels(id)>4) && num[parent(id)]<0) return id
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
@ -97,25 +104,65 @@ function dosplit(id){
|
||||||
}
|
}
|
||||||
function tosplit(){
|
function tosplit(){
|
||||||
for(id=1;id<=pindex;id++){
|
for(id=1;id<=pindex;id++){
|
||||||
print id, (num[id]-10>0), num[id]
|
# print id, (num[id]-10>0), num[id]
|
||||||
if((num[id]-10>0)) return id
|
if((num[id]-10>=0) && num[parent(id)]<0 ) return id
|
||||||
}
|
}
|
||||||
return -1
|
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 }
|
BEGIN{ pindex = 0 }
|
||||||
|
|
||||||
{
|
NR==1{
|
||||||
parse( $0 )
|
parse($0)
|
||||||
if(NR>1){
|
|
||||||
num[++pindex] = -1
|
|
||||||
ile[pindex] = proot
|
|
||||||
iri[pindex] = root
|
|
||||||
root = pindex
|
|
||||||
}
|
|
||||||
proot = root
|
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{
|
END{
|
||||||
OFS=":"
|
OFS=":"
|
||||||
print pindex, root
|
print pindex, root
|
||||||
|
@ -128,27 +175,14 @@ END{
|
||||||
|
|
||||||
# print num[1], "is at",levels(1),"levels"
|
# print num[1], "is at",levels(1),"levels"
|
||||||
|
|
||||||
|
s = treetostring(root)
|
||||||
|
cleanparse(s)
|
||||||
print treetostring(root)
|
print treetostring(root)
|
||||||
|
|
||||||
c = 0;
|
reduce()
|
||||||
do{
|
|
||||||
ind = toexplode()
|
|
||||||
print "explode?", ind
|
|
||||||
|
|
||||||
if(ind>0){
|
print treetostring(root)
|
||||||
explode(ind)
|
|
||||||
print "exploded"
|
print magnitude(root)
|
||||||
}
|
|
||||||
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"
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue