day 11, parts 1 & 2, cleaner

This commit is contained in:
sejo 2021-12-11 12:45:02 -06:00
parent 73fd80dfb6
commit cd213c0eea
1 changed files with 41 additions and 0 deletions

41
11/11_new.awk Normal file
View File

@ -0,0 +1,41 @@
BEGIN { FS = "" }
{ for(i=1;i<=NF;i++) o[NR,i] = $i }
END{
s=1
while(nflashes!=NF*NR){
nflashes = 0
delete visited
for(i in o) o[i]++
do{
for(y=1;y<=NR;y++){
for(x=1;x<=NF;x++){
if(!visited[y,x] && o[y,x]>9){
if(y>1 && x>1) o[y-1,x-1]++
if(y>1 && x<NF) o[y-1,x+1]++
if(y>1) o[y-1,x]++
if(x>1) o[y,x-1]++
if(x<NF) o[y,x+1]++
if(y<NR && x>1) o[y+1,x-1]++
if(y<NR && x<NF) o[y+1,x+1]++
if(y<NR) o[y+1,x]++
visited[y,x] = 1
}
}
}
tochange=0
for(i in o) tochange+=(o[i]>9) && !visited[i]
} while(tochange)
for(i in o ){
if( o[i]>9 ){ o[i] = 0; nflashes++ }
}
flashes += nflashes
if(s==100) print "part 1:", flashes
s++
}
print "part 2: step", s-1
}