day 17 part one
[advent21.git] / 17a.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <string.h>
5
6 bool is_in(int dx, int dy, int x1, int x2, int y1, int y2, int *maxy)
7 {
8 int x = 0, y = 0, my = 0;
9 while (y >= y2 && x <= x2) {
10 x+=dx;
11 y+=dy;
12 dx = dx == 0 ? 0 : dx-1;
13 dy--;
14 if (y > my)
15 my = y;
16 if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
17 if (my > *maxy)
18 *maxy = my;
19 return true;
20 }
21 }
22 return false;
23 }
24
25 int main()
26 {
27 size_t len;
28 char *buf = NULL;
29 if (getline(&buf, &len, stdin) == -1)
30 return 1;
31 printf("%s", buf);
32
33 while (*buf++ != '=');
34
35 int x1 = strtol(buf, &buf, 10);
36 buf+=strlen("..");
37 int x2 = strtol(buf, &buf, 10);
38 buf+=strlen(", y=");
39 int y1 = strtol(buf, &buf, 10);
40 buf+=strlen("..");
41 int y2 = strtol(buf, &buf, 10);
42
43 int maxy = 0;
44 for (int dy = x2; dy >= 0; dy--)
45 for (int dx = 1; dx <= x2; dx++)
46 if (is_in(dx, dy, x1, x2, y1, y2, &maxy))
47 break;
48 printf("%d\n", maxy);
49 }