2
1
Fork 0

Day 11: Add C solution

This commit is contained in:
Mia Herkt 2021-12-12 05:57:57 +01:00
parent faf3fd1358
commit 5c6f265376
Signed by: mia
GPG Key ID: 72E154B8622EC191
2 changed files with 39 additions and 0 deletions

10
11/input.txt Normal file
View File

@ -0,0 +1,10 @@
7777838353
2217272478
3355318645
2242618113
7182468666
5441641111
4773862364
5717125521
7542127721
4576678341

29
11/solution.c Normal file
View File

@ -0,0 +1,29 @@
#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 ;}