From 02524fae8d6def36070fa99101f5c7e437812dca Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sun, 5 Dec 2021 12:48:57 +0100 Subject: [PATCH] speed up a bit --- 05a.c | 20 +++++++++++++------- 05b.c | 27 +++++++++++++++++---------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/05a.c b/05a.c index f8d3421..3e5b008 100644 --- a/05a.c +++ b/05a.c @@ -2,7 +2,7 @@ #include #include -struct line { int x1; int y1; int x2; int y2; }; +struct line { enum {horz, vert} d; int x1; int y1; int x2; int y2; }; #define max(x, y) ((x)>(y) ? (x) : (y)) #define min(x, y) ((x)<(y) ? (x) : (y)) @@ -21,6 +21,12 @@ int parse_lines(struct line lines[], int *maxx, int *maxy) lines[i].x2 = strtol(p, &p, 10); p++; lines[i].y2 = strtol(p, &p, 10); + if (lines[i].x1 == lines[i].x2) + lines[i].d = vert; + else if (lines[i].y1 == lines[i].y2) + lines[i].d = horz; + else + continue; *maxx = max(*maxx, lines[i].x1); *maxx = max(*maxx, lines[i].x2); *maxy = max(*maxy, lines[i].y1); @@ -32,12 +38,12 @@ int parse_lines(struct line lines[], int *maxx, int *maxy) bool online(int x, int y, struct line line) { - //Vertical - if (line.x1 == line.x2 && x == line.x1) - return between(y, line.y1, line.y2); - //Horizontal - if (line.y1 == line.y2 && y == line.y2) - return between(x, line.x1, line.x2); + switch (line.d) { + case vert: + return x == line.x1 && between(y, line.y1, line.y2); + case horz: + return y == line.y1 && between(x, line.x1, line.x2); + } return false; } diff --git a/05b.c b/05b.c index d7ca591..208f870 100644 --- a/05b.c +++ b/05b.c @@ -2,7 +2,7 @@ #include #include -struct line { int x1; int y1; int x2; int y2; }; +struct line { enum {horz, vert, diag} d; int x1; int y1; int x2; int y2; }; #define max(x, y) ((x)>(y) ? (x) : (y)) #define min(x, y) ((x)<(y) ? (x) : (y)) @@ -21,6 +21,14 @@ int parse_lines(struct line lines[], int *maxx, int *maxy) lines[i].x2 = strtol(p, &p, 10); p++; lines[i].y2 = strtol(p, &p, 10); + if (lines[i].x1 == lines[i].x2) + lines[i].d = vert; + else if (lines[i].y1 == lines[i].y2) + lines[i].d = horz; + else if (abs(lines[i].y2-lines[i].y1) == abs(lines[i].x2-lines[i].x1)) + lines[i].d = diag; + else + continue; *maxx = max(*maxx, lines[i].x1); *maxx = max(*maxx, lines[i].x2); *maxy = max(*maxy, lines[i].y1); @@ -32,15 +40,14 @@ int parse_lines(struct line lines[], int *maxx, int *maxy) bool online(int x, int y, struct line line) { - //Vertical - if (line.x1 == line.x2 && x == line.x1) - return between(y, line.y1, line.y2); - //Horizontal - if (line.y1 == line.y2 && y == line.y2) - return between(x, line.x1, line.x2); - //Diagonal - if (abs(line.y2-line.y1) == abs(line.x2-line.x1)) { - int slope = (line.y2 - line.y1) / (line.x2 - line.x1); + int slope; + switch (line.d) { + case horz: + return y == line.y1 && between(x, line.x1, line.x2); + case vert: + return x == line.x1 && between(y, line.y1, line.y2); + case diag: + slope = (line.y2 - line.y1) / (line.x2 - line.x1); return y-line.y1 == slope*(x-line.x1) && between(y, line.y1, line.y2) && between(x, line.x1, line.x2); } -- 2.20.1