2
1
Fork 0
aoc2021/08/solution.c

38 lines
2.4 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define I( N,n) (char*) &N[ n ]
int main(){char*d[9] ={0},e[10][8],o[5][8
#define Q(c) p1=o[i];F(c ,p1,t)if((f=(int)strlen\
(t )==l -2 ))
],eg[3 ],bd[3 ],t[8] ,*p,*p1
,*p2,* a,*b;int i,f,l, u[8]={
0,0,1, 7,4,0, 0,8},S =0,G=0
;while (scanf ("%s " "%s %"
"s %s " "%s %" "s %s" "%s %s"
" %s |" " %s " "%s %" "s %s"
#define F(a,b ,c) p= c;for\
(p2 = b; *p2 ; p2++ ){ f=\
0;for( p1=a;* p1; p1 ++)if\
(f+=*p1 ==*p2) break; if(!f\
)*p++ =*p2 ;}*p =0;
,I (e ,0 ),I(e,1),I(e,2),I(e, 3)
#define D(n) o[4][i]=n+'0';
,I (e ,4 ),I(e,5),I(e,6),I(e, 7)
,I(e ,8), I(e, 9),I
(o,0), I(o,1) ,I(o,2 ),I(o,
3))==14 ){for( i=0;i< 10;i++
){p=e[ i];d[u [strlen (p) ]]
=p;}a= d[7];b =d[4]; strcpy
( t, a ); F ( a,b,t+ strlen
(a)) F (t,d[8 ],eg)F (d [1]
,d[4], bd) for (i=0; i<4 ;i
++) {D (0) l= strlen (o[i])
;if (u [l]) { S++;o[ 4][i]=
u[l]+'0' ;}else if (l ==5){Q
( bd )D (5
) else{Q(eg)D(2)else D(3 )}}else if(l==6){Q(eg){Q
(bd)D(6)}else D(9)}} G+=atoi(o[4]);}printf
("Silver: %d\nGo" "ld: %d\n",S,G);}