day 22, pt 1

This commit is contained in:
sejo 2021-12-21 23:49:05 -06:00
parent 64da1ce6d1
commit 51f807fbfe
3 changed files with 101 additions and 0 deletions

37
22/22.awk Normal file
View File

@ -0,0 +1,37 @@
{
state = ($1=="on") ? 1 : 0
split($2,arr,",")
split(arr[1],x,/\.\./)
split(arr[2],y,/\.\./)
split(arr[3],z,/\.\./)
sub(/^.=/,"",x[1])
sub(/^.=/,"",y[1])
sub(/^.=/,"",z[1])
print x[1],x[2],y[1],y[2],z[1],z[2], state
c=0
iz = (strtonum(z[1])<-50) ? -50 : strtonum(z[1])
iy = (strtonum(y[1])<-50) ? -50 : strtonum(y[1])
ix = (strtonum(x[1])<-50) ? -50 : strtonum(x[1])
fz = (strtonum(z[2])>50) ? 50 : strtonum(z[2])
fy = (strtonum(y[2])>50) ? 50 : strtonum(y[2])
fx = (strtonum(x[2])>50) ? 50 : strtonum(x[2])
print iz
for(tz=iz;tz<=fz;tz++){
for(ty=iy;ty<=fy;ty++){
for(tx=ix;tx<=fx;tx++){
# printf "%d,%d,%d:%d\n", tx,ty,tz,state
cube[tx,ty,tz] = state;
c++
}
}
}
print c
}
END{
for(p in cube)
count += cube[p]
print count
}

50
22/22_2.awk Normal file
View File

@ -0,0 +1,50 @@
function size( cu ){
split(cu, c, ",")
return (c[2]-c[1]+1)*(c[4]-c[3]+1)*(c[6]-c[5]+1)
}
function cubestate( cu ){
return substr(cu,length(cu))
}
function intersect(cube1, cube2){
split(cube1,c1,",")
split(cube2,c2,",")
difx = (c1[1]<c2[1]) ? c1[2]-c2[1] + 1 : c2[2] - c1[1] + 1
dify = (c1[3]<c2[3]) ? c1[4]-c2[3] + 1 : c2[4] - c1[3] + 1
difz = (c1[5]<c2[5]) ? c1[6]-c2[5] + 1 : c2[6] - c1[5] + 1
print difx, dify, difz
if(difx>0 && dify>0 && difz>0)
return difx*dify*difz
else
return 0
}
{
state = ($1=="on") ? 1 : 0
split($2,arr,",")
split(arr[1],x,/\.\./)
split(arr[2],y,/\.\./)
split(arr[3],z,/\.\./)
sub(/^.=/,"",x[1])
sub(/^.=/,"",y[1])
sub(/^.=/,"",z[1])
x[1] = strtonum(x[1])
y[1] = strtonum(y[1])
z[1] = strtonum(z[1])
cube[NR] = x[1] "," x[2] "," y[1] "," y[2] "," z[1] "," z[2] "," state
print cube[NR], size(cube[NR]), cubestate(cube[NR])
}
END{
print ""
for(i=1;i<NR;i++){
for(j=i+1;j<=NR;j++){
ints[i,j] = intersect(cube[i],cube[j])
}
}
}

14
22/Makefile Normal file
View File

@ -0,0 +1,14 @@
1t:
awk -f 22.awk test
1i:
awk -f 22.awk input
2t:
awk -f 22_2.awk test
2i:
awk -f 22_2.awk input
paste:
xclip -sel c -o > test