2
1
Fork 0
aoc2021/11/solution.c

30 lines
1.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char*O=NULL;typedef
size_t S;typedef unsigned char
U ;S n=0;void V(){S x,y;U*B=malloc(n*
n *3);printf("P6\n%lu %lu\n255\n",n,n);
#pragma /* passing any cmd line argument */\
omp parallel /* will enable netpbm stream output */ for
for(y=0;y <n;y++){for(x=0;x<n;x++){float v=((O[y*n+x] ==0?12
:O[y*n+x] )*0.75)/10.0;U*b=B+y*n*3+x*3;*b++=250*v/3;*b ++=255*
v;*b++= 240 * v/2;}} fwrite(B ,n*n*3,1, stdout);
free(B ) ;}extern int I(S j,S**q); int main(int
h, char**){char*B;S x,y=0,f=0,F=0,*Q =
NULL,*q;for(; scanf("%ms",&B )==1;y++){if(!n){n
=strlen(B);O=malloc(n*n );Q=malloc(n*n*5*sizeof(S));}for(
x=0;x<n;x++)O[y*n+x]=B[x]-'0';free(B);}for(y=0;f<n*n;y++){
#pragma /* build w/ pgo + openmp for speed */ omp parallel for
for(x=0;x<n*n;x++)O[x]++;f=0;q=Q;for(x=0;x<n*n;x++)f+=I(x,
& q);while(q>Q){S p=*(q-1);f+=I(p,&q);q--;}F+=f;if(h>1)V(O,
n);else{if(y&&y%100==0)puts("");printf("\033[KStep %lu: "
"%lu Flashes\n\033[F",y+1,F);}}puts("");free(O);free(Q);}
inline int I( S j,S**q){if(O[j]>9){int x,y,X=(j%n!=n-1),
Y=(j/ n!=n-1); O[j]=0;for(y=- (j/n!=0);y<=Y;y++){
for(x=-(j% n!=0);x<=X ;x ++){S e=y*n+x+j;if
(O [e]){O[e ]++;*(*q )++=e;}}}*(*q
)++=j; return 1;}return
0 ;}