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>
#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);
}
}

View file

@ -1,4 +1,5 @@
#include <stdio.h>
#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);
}
}

View file

@ -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<b?1:-1
#define L(s,e)u S=P(s,e);for(u c=s;c!=e;c+=S)
int
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);};};for(y=
0;y<O;y++)for(x=0;x<O;x++)I
+=m[x][y]
>1;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);}