day 17 part one
[advent21.git] / 17a.c
diff --git a/17a.c b/17a.c
new file mode 100644 (file)
index 0000000..2140026
--- /dev/null
+++ b/17a.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+
+bool is_in(int dx, int dy, int x1, int x2, int y1, int y2, int *maxy)
+{
+       int x = 0, y = 0, my = 0;
+       while (y >= y2 && x <= x2) {
+               x+=dx;
+               y+=dy;
+               dx = dx == 0 ? 0 : dx-1;
+               dy--;
+               if (y > my)
+                       my = y;
+               if (x >= x1 && x <= x2 && y >= y1 && y <= y2)  {
+                       if (my > *maxy)
+                               *maxy = my;
+                       return true;
+               }
+       }
+       return false;
+}
+
+int main()
+{
+       size_t len;
+       char *buf = NULL;
+       if (getline(&buf, &len, stdin) == -1)
+               return 1;
+       printf("%s", buf);
+
+       while (*buf++ != '=');
+
+       int x1 = strtol(buf, &buf, 10);
+       buf+=strlen("..");
+       int x2 = strtol(buf, &buf, 10);
+       buf+=strlen(", y=");
+       int y1 = strtol(buf, &buf, 10);
+       buf+=strlen("..");
+       int y2 = strtol(buf, &buf, 10);
+
+       int maxy = 0;
+       for (int dy = x2; dy >= 0; dy--)
+               for (int dx = 1; dx <= x2; dx++)
+                       if (is_in(dx, dy, x1, x2, y1, y2, &maxy))
+                               break;
+       printf("%d\n", maxy);
+}