day 20, part 1

This commit is contained in:
sejo 2021-12-20 12:05:43 -06:00
parent 23e0160b98
commit b4b2a6b675
2 changed files with 92 additions and 0 deletions

78
20/20.awk Normal file
View File

@ -0,0 +1,78 @@
BEGIN{ RS = ""; FS="" }
NR==1{
for(i=1;i<=NF;i++){
en[i-1] = $i=="#" ? 1 : 0
}
RS = "\n"
next
}
{
for(i=-1;i<=NF+2;i++){
if(i>=1 && i<=NF)
inp[i,NR-1] = $i=="#" ? 1 : 0
else
inp[i,NR-1] = 0
}
}
function wintonum(px,py){
nbit = 8; num=0
for(j=py-1;j<=py+1;j++){
for(i=px-1;i<=px+1;i++){
b = inp[i,j]=="" ? 0 : inp[i,j]
num += lshift( b, nbit)
nbit--
}
}
return num
}
function wintonum2(px,py){
nbit = 8; num=0
for(j=py-1;j<=py+1;j++){
for(i=px-1;i<=px+1;i++){
b = inp[i,j]=="" ? en[0] : inp[i,j]
num += lshift( b, nbit)
nbit--
}
}
return num
}
END{
xmin=-1; xmax=NF+2
ymin=-1; ymax=NR+1
count = 0
# first pass
for(y=ymin; y<=ymax; y++){
for(x=xmin; x<=xmax; x++){
newinp[x,y] = en[ wintonum(x,y) ]
count += en[ wintonum(x,y) ]
# printf "%c", newinp[x,y]==1? "#" : "."
}
# print ""
}
print count
print ""
delete inp
# copy result
for(p in newinp) inp[p] = newinp[p]
# second pass
count = 0
for(y=ymin; y<=ymax; y++){
for(x=xmin; x<=xmax; x++){
wn = wintonum2(x,y)
count += en[ wn ]
# printf "%c", en[ wn ]==1 ? "#" : "."
}
# print ""
}
print count
}

14
20/Makefile Normal file
View File

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