speed up a bit
[advent21.git] / 05a.c
diff --git a/05a.c b/05a.c
index f8d3421..3e5b008 100644 (file)
--- a/05a.c
+++ b/05a.c
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 
-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;
 }