2
1
Fork 0

Day 5/C: Simplify

This commit is contained in:
Mia Herkt 2021-12-06 00:31:09 +01:00
parent a980f49d42
commit a47b1aff5b
Signed by: mia
GPG key ID: 72E154B8622EC191
3 changed files with 35 additions and 51 deletions

View file

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#define put(x, y) intersections += ++map[x][y] == 2
int main(void) { int main(void) {
FILE *fp = fopen("input.txt", "r"); FILE *fp = fopen("input.txt", "r");
@ -11,24 +12,15 @@ int main(void) {
if (x0 == x1) { if (x0 == x1) {
if (y0 > y1) if (y0 > y1)
inc = -1; inc = -1;
for (unsigned y = y0; y != y1; y += inc) { for (unsigned y = y0; y != y1; y += inc)
map[x0][y]++; put(x0, y);
} put(x0, y1);
map[x0][y1]++;
} else if (y0 == y1) { } else if (y0 == y1) {
if (x0 > x1) if (x0 > x1)
inc = -1; inc = -1;
for (unsigned x = x0; x != x1; x += inc) { for (unsigned x = x0; x != x1; x += inc)
map[x][y0]++; put(x, y0);
} put(x1, y0);
map[x1][y0]++;
}
}
for (unsigned y = 0; y < 1000; y++) {
for (unsigned x = 0; x < 1000; x++) {
if (map[x][y] > 1)
intersections++;
} }
} }

View file

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#define put(x, y) intersections += ++map[x][y] == 2
int main(void) { int main(void) {
FILE *fp = fopen("input.txt", "r"); FILE *fp = fopen("input.txt", "r");
@ -10,30 +11,21 @@ int main(void) {
char incY = y0 < y1 ? 1 : -1; char incY = y0 < y1 ? 1 : -1;
char incX = x0 < x1 ? 1 : -1; char incX = x0 < x1 ? 1 : -1;
if (x0 == x1) { if (x0 == x1) {
for (unsigned y = y0; y != y1; y += incY) { for (unsigned y = y0; y != y1; y += incY)
map[x0][y]++; put(x0, y);
} put(x0, y1);
map[x0][y1]++;
} else if (y0 == y1) { } else if (y0 == y1) {
for (unsigned x = x0; x != x1; x += incX) { for (unsigned x = x0; x != x1; x += incX)
map[x][y0]++; put(x, y0);
} put(x1, y0);
map[x1][y0]++;
} else { } else {
unsigned x = x0, y = y0; unsigned x = x0, y = y0;
while (x != x1 && y != y1) { while (x != x1 && y != y1) {
map[x][y]++; put(x, y);
x += incX; x += incX;
y += incY; y += incY;
} }
map[x][y]++; put(x, y);
}
}
for (unsigned y = 0; y < 1000; y++) {
for (unsigned x = 0; x < 1000; x++) {
if (map[x][y] > 1)
intersections++;
} }
} }

View file

@ -2,24 +2,24 @@
#define O 1000 #define O 1000
#define E(a,b,X)if(a==b){X}else #define E(a,b,X)if(a==b){X}else
#define u int #define u int
#define C(x,y)m[x][y]++ #define C(x,y)I+=++m[x][y]==2;
#define P(a,b)a<b?1:-1 #define P(a,b)a<b?1:-1
#define L(s,e)u S=P(s,e);for(u c=s;c!=e;c+=S) #define L(s,e)u S=P(s,e);for(u c=s;c!=e;c+=S)
int u
main( main
void){u m (void){u
[O][O]={0},x, m[O][O]={0},
X,y,Y,I x,X,y,Y,
=0;while( I=0;while(
fscanf( stdin, fscanf( stdin,
"%i,%i -> %i,%i",&x "%i,%i -> %i,%i",&
,&y,&X,&Y)==4){E(x,X,L(y, x,&y,&X,&Y)==4){E(x,X,L
Y)C(x,c);C(x,Y);) (y,Y)C(x,c)C(x,Y)
E(y,Y,L(x,X)C(c,y); )E(y,Y,L(x,X)C(c,y)C
C(X,y);){u h=x,v=y,H=P( (X,y)){u h=x,v=y,H=P(x,X
x,X),V=P(y,Y);for(;h!=X&&v!=Y ),V=P(y,Y);for(;h!=X&&v!=Y;h+=
;h+=H,v+=V)C(h,v);C(h,v);};};for(y= H,v+=V
0;y<O;y++)for(x=0;x<O;x++)I )C(h,v
+=m[x][y] )C(h,v
>1;printf )}}printf(
("Answer: %d\n",I);;} "Answer: %d\n",I);}