Compare commits

...

3 Commits

Author SHA1 Message Date
sejo c85d898254 stats day 20 2021-12-20 12:16:07 -06:00
sejo 89d71d4946 day 2, part 2! 2021-12-20 12:14:52 -06:00
sejo b4b2a6b675 day 20, part 1 2021-12-20 12:05:43 -06:00
4 changed files with 161 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
}

68
20/20_2.awk Normal file
View File

@ -0,0 +1,68 @@
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,pss){
nbit = 8; num=0
for(j=py-1;j<=py+1;j++){
for(i=px-1;i<=px+1;i++){
b = inp[i,j]=="" ? (pss%2==0? en[0] : 0 ) : inp[i,j]
num += lshift( b, nbit)
nbit--
}
}
return num
}
END{
xmin=-1; xmax=NF+2
ymin=-1; ymax=NR+1
for(pass=1;pass<=50;pass++){
count = 0
for(y=ymin-pass; y<=ymax+pass; y++){
for(x=xmin-pass; x<=xmax+pass; x++){
wn = (pass==1) ? wintonum(x,y) : wintonum2(x,y,pass)
newinp[x,y] = en[ wn ]
count += en[ wn ]
# printf "%c", newinp[x,y]==1? "#" : "."
}
# print ""
}
print "pass",pass,"count:",count
delete inp
# copy result
for(p in newinp) inp[p] = newinp[p]
delete newinp
}
}

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

View File

@ -27,6 +27,7 @@ awk -c -f 01.awk inputfile
```
-------Part 1-------- -------Part 2--------
Day Time Rank Score Time Rank Score
20 12:59:28 11947 0 13:14:02 11669 0 --- same as 18
18 >24h 16659 0 >24h 16484 0 --- solved it afterwards
17 00:41:03 2839 0 00:56:57 2841 0
16 00:34:15 550 0 02:21:09 3657 0