2
1
Fork 0
aoc2021/13/solution.c
2021-12-13 15:40:47 +01:00

40 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Q
#define U\
x=D[i] >> 32, \
y= D[i] & 0xffffffff
(const void * A, const
void*B){int64_t a= *(int64_t
*) A,b=*(
int64_t*)B;
return a>b?a
<b?-1:0:1;}
int main(){
size_t n=0,a=
500,i,l,X=0,Y /***/
=0,t=0;char c /*********/
;int64_t*D= /***************/
malloc(sizeof( /*******************/
int64_t)*a),L,R /**********************/
,x,y;while(scanf /************************/
("%ld,%ld\n",&L, /************************/
&R)==2){if(n+1> /************************/
a)D= realloc(D, /*************/ sizeof(
int64_t)*(a+=500)) /**/ ;D[n++]=(L<<32) |R;}
while(scanf("%*" "[^xy]%c=%ld\n",&c,&L )==2)
{for(i =0;i<n;i++){U;if(c=='x'&&x>L) x=2*L-x
;else if(c=='y'&&y>L)y=2*L-y;D[i]= y|(x<<32)
;}qsort(D,n,sizeof(int64_t),Q);for(i=1,l=D[0 ];i<n;i++)
{if(D[i]==l)D[ i ]=-1; l=D[i];}qsort(D ,n ,sizeof(
int64_t),Q);for(i=n;i>0;i--){if(D[i ]==-1)n=i;}if(
!t)t=n;}printf("%lu dots left " "after first fol"
"d instruction\n",t);for( i=0;i<n;i++){U;x++
;y++;X=x>X ?x:X;Y=y>Y ?y:Y;}char O[X*Y];
memset(O,' ',X*Y );for(i=0;i<n;i++){U;O
[y*X+x]='|'; }free(D);for(i=0;i<Y;i++
)printf( "%.*s\n" ,(int)X,O+i*X);}