aboutsummaryrefslogtreecommitdiff
path: root/2021/17
diff options
context:
space:
mode:
authorMarvin Borner2021-12-17 14:39:50 +0100
committerMarvin Borner2021-12-17 14:39:50 +0100
commit1ebf6509c67873774b6c080bfa8a3eeb42043a84 (patch)
tree5232b97a6e0759bf499579d022a678f2d9549fd1 /2021/17
parent9552e3981a312d207252c8e2993a90d87d54167b (diff)
kk
Diffstat (limited to '2021/17')
-rw-r--r--2021/17/Makefile19
-rw-r--r--2021/17/input1
-rw-r--r--2021/17/solve.c68
3 files changed, 88 insertions, 0 deletions
diff --git a/2021/17/Makefile b/2021/17/Makefile
new file mode 100644
index 0000000..4a881b7
--- /dev/null
+++ b/2021/17/Makefile
@@ -0,0 +1,19 @@
+DEBUG = -Wno-error -Og -g -s -fsanitize=undefined -fsanitize=address -fstack-protector-all
+CFLAGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors -Ofast
+
+# Not the best makefile but idc
+
+debug:
+ @gcc $(CFLAGS) $(DEBUG) solve.c -o solve.o -L/usr/lib -lcrypto
+
+build:
+ @gcc $(CFLAGS) solve.c -o solve.o -L/usr/lib -lcrypto
+
+clean:
+ @rm -f *.o
+
+run: debug
+ @./solve.o
+
+time: build
+ @./solve.o
diff --git a/2021/17/input b/2021/17/input
new file mode 100644
index 0000000..52bdf2b
--- /dev/null
+++ b/2021/17/input
@@ -0,0 +1 @@
+target area: x=175..227, y=-134..-79
diff --git a/2021/17/solve.c b/2021/17/solve.c
new file mode 100644
index 0000000..9d7ba51
--- /dev/null
+++ b/2021/17/solve.c
@@ -0,0 +1,68 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+typedef struct {
+ int x, y;
+} vec2;
+
+typedef struct {
+ vec2 min;
+ vec2 max;
+} range;
+
+#define IN_RANGE(pos, range) \
+ (pos.x >= range.min.x && pos.x <= range.max.x && pos.y >= range.min.y && \
+ pos.y <= range.max.y)
+#define ADD(a, b) (a = (vec2){ a.x + b.x, a.y + b.y })
+#define DRAG(x) ((x) < 0 ? (x)++ : (x) > 0 ? (x)-- : (x))
+
+static int hits(vec2 velocity, range target)
+{
+ vec2 pos = { 0 };
+ while (ADD(pos, velocity), DRAG(velocity.x), velocity.y--, pos.y >= target.min.y)
+ if (IN_RANGE(pos, target))
+ return 1; // Hit!
+ return 0;
+}
+
+static void solve(FILE *fp)
+{
+ int first = 0, second = 0;
+
+ range target;
+ fscanf(fp, "target area: x=%d..%d, y=%d..%d\n", &target.min.x, &target.max.x, &target.min.y,
+ &target.max.y);
+
+ vec2 velocity;
+ for (velocity.y = target.min.y; velocity.y <= -target.min.y; velocity.y++) {
+ for (velocity.x = 0; velocity.x <= target.max.x; velocity.x++) {
+ if (hits(velocity, target)) {
+ second++;
+ int height = velocity.y * (velocity.y + 1) / 2;
+ if (height > first)
+ first = height;
+ }
+ }
+ }
+
+ printf("%d\n%d\n", first, second);
+}
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ FILE *fp = fopen("input", "r");
+ if (!fp)
+ exit(EXIT_FAILURE);
+
+ clock_t tic = clock();
+ solve(fp);
+ clock_t toc = clock();
+ printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
+
+ fclose(fp);
+ return 0;
+}