From a47b1aff5bf784a4ae213e760a011c0661e2b053 Mon Sep 17 00:00:00 2001 From: Mia Herkt Date: Mon, 6 Dec 2021 00:31:09 +0100 Subject: [PATCH] Day 5/C: Simplify --- 05/solution_1.c | 22 +++++++--------------- 05/solution_2.c | 26 +++++++++----------------- 05/solution_2_tree.c | 38 +++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 51 deletions(-) diff --git a/05/solution_1.c b/05/solution_1.c index 4721ee0..4bade35 100644 --- a/05/solution_1.c +++ b/05/solution_1.c @@ -1,4 +1,5 @@ #include +#define put(x, y) intersections += ++map[x][y] == 2 int main(void) { FILE *fp = fopen("input.txt", "r"); @@ -11,24 +12,15 @@ int main(void) { if (x0 == x1) { if (y0 > y1) inc = -1; - for (unsigned y = y0; y != y1; y += inc) { - map[x0][y]++; - } - map[x0][y1]++; + for (unsigned y = y0; y != y1; y += inc) + put(x0, y); + put(x0, y1); } else if (y0 == y1) { if (x0 > x1) inc = -1; - for (unsigned x = x0; x != x1; x += inc) { - map[x][y0]++; - } - map[x1][y0]++; - } - } - - for (unsigned y = 0; y < 1000; y++) { - for (unsigned x = 0; x < 1000; x++) { - if (map[x][y] > 1) - intersections++; + for (unsigned x = x0; x != x1; x += inc) + put(x, y0); + put(x1, y0); } } diff --git a/05/solution_2.c b/05/solution_2.c index d593176..f9729ec 100644 --- a/05/solution_2.c +++ b/05/solution_2.c @@ -1,4 +1,5 @@ #include +#define put(x, y) intersections += ++map[x][y] == 2 int main(void) { FILE *fp = fopen("input.txt", "r"); @@ -10,30 +11,21 @@ int main(void) { char incY = y0 < y1 ? 1 : -1; char incX = x0 < x1 ? 1 : -1; if (x0 == x1) { - for (unsigned y = y0; y != y1; y += incY) { - map[x0][y]++; - } - map[x0][y1]++; + for (unsigned y = y0; y != y1; y += incY) + put(x0, y); + put(x0, y1); } else if (y0 == y1) { - for (unsigned x = x0; x != x1; x += incX) { - map[x][y0]++; - } - map[x1][y0]++; + for (unsigned x = x0; x != x1; x += incX) + put(x, y0); + put(x1, y0); } else { unsigned x = x0, y = y0; while (x != x1 && y != y1) { - map[x][y]++; + put(x, y); x += incX; y += incY; } - map[x][y]++; - } - } - - for (unsigned y = 0; y < 1000; y++) { - for (unsigned x = 0; x < 1000; x++) { - if (map[x][y] > 1) - intersections++; + put(x, y); } } diff --git a/05/solution_2_tree.c b/05/solution_2_tree.c index d92057e..0ffefd0 100644 --- a/05/solution_2_tree.c +++ b/05/solution_2_tree.c @@ -2,24 +2,24 @@ #define O 1000 #define E(a,b,X)if(a==b){X}else #define u int -#define C(x,y)m[x][y]++ +#define C(x,y)I+=++m[x][y]==2; #define P(a,b)a %i,%i",&x - ,&y,&X,&Y)==4){E(x,X,L(y, - Y)C(x,c);C(x,Y);) - E(y,Y,L(x,X)C(c,y); - C(X,y);){u h=x,v=y,H=P( - x,X),V=P(y,Y);for(;h!=X&&v!=Y - ;h+=H,v+=V)C(h,v);C(h,v);};};for(y= - 0;y1;printf - ("Answer: %d\n",I);;} + u + main + (void){u + m[O][O]={0}, + x,X,y,Y, + I=0;while( + fscanf( stdin, + "%i,%i -> %i,%i",& + x,&y,&X,&Y)==4){E(x,X,L + (y,Y)C(x,c)C(x,Y) + )E(y,Y,L(x,X)C(c,y)C + (X,y)){u h=x,v=y,H=P(x,X + ),V=P(y,Y);for(;h!=X&&v!=Y;h+= + H,v+=V + )C(h,v + )C(h,v + )}}printf( + "Answer: %d\n",I);}