day 17 part one
authorMart Lubbers <mart@martlubbers.net>
Fri, 17 Dec 2021 08:11:51 +0000 (09:11 +0100)
committerMart Lubbers <mart@martlubbers.net>
Fri, 17 Dec 2021 08:11:51 +0000 (09:11 +0100)
16b.c
17.txt [new file with mode: 0644]
17a.c [new file with mode: 0644]
17e.txt [new file with mode: 0644]

diff --git a/16b.c b/16b.c
index 437560e..6c93a16 100644 (file)
--- a/16b.c
+++ b/16b.c
@@ -14,13 +14,8 @@ struct stream { int pos; char *buf; };
 int next(struct stream *f)
 {
        int r;
-       if (*f->buf == '\0') {
-               if ((r = getchar()) == EOF) {
-                       printf("EOF\n");
-                       exit(1);
-               }
+       if (*f->buf == '\0')
                f->buf = hex2bin[r];
-       }
        r =*(f->buf++) == '1' ? 1 : 0;
        f->pos++;
        return r;
diff --git a/17.txt b/17.txt
new file mode 100644 (file)
index 0000000..58c356f
--- /dev/null
+++ b/17.txt
@@ -0,0 +1 @@
+target area: x=150..193, y=-136..-86
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);
+}
diff --git a/17e.txt b/17e.txt
new file mode 100644 (file)
index 0000000..a07e02d
--- /dev/null
+++ b/17e.txt
@@ -0,0 +1 @@
+target area: x=20..30, y=-10..-5