added day 17 of aoc-2021
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Dec 2021 15:21:37 +0000 (09:21 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Dec 2021 15:21:37 +0000 (09:21 -0600)
tests/aoc-2021/day17 [new file with mode: 0644]
tests/aoc-2021/day17.onyx [new file with mode: 0644]
tests/aoc-2021/input/day17.txt [new file with mode: 0644]

diff --git a/tests/aoc-2021/day17 b/tests/aoc-2021/day17
new file mode 100644 (file)
index 0000000..b2df517
--- /dev/null
@@ -0,0 +1,2 @@
+Part 1: 15400
+Part 2: 5844
diff --git a/tests/aoc-2021/day17.onyx b/tests/aoc-2021/day17.onyx
new file mode 100644 (file)
index 0000000..03d9f67
--- /dev/null
@@ -0,0 +1,61 @@
+#load "core/std"
+
+use package core
+
+tx0: i32
+ty0: i32
+tx1: i32
+ty1: i32
+
+simulate :: (dx, dy: i32) -> (i32, bool) {
+    x, y := 0, 0;
+    maxy := 0;
+    did_enter := false;
+    while y >= math.min(ty1, ty0) && x <= math.max(tx1, tx0) {
+        x += dx;
+        y += dy;
+        if dx > 0 do dx -= 1;
+        if dx < 0 do dx += 1;
+        dy -= 1;
+
+        maxy = math.max(y, maxy);
+        if tx0 <= x && x <= tx1 && ty0 <= y && y <= ty1 {
+            did_enter = true;
+        }
+    }
+
+    return maxy, did_enter;
+}
+
+
+main :: (args) => {
+
+    for file: os.with_file("./tests/aoc-2021/input/day17.txt") {
+        reader := io.reader_make(file);
+
+        io.skip_bytes(^reader, 15);
+        tx0 = io.read_i32(^reader);
+        io.skip_bytes(^reader, 2);
+        tx1 = io.read_i32(^reader);
+        io.skip_bytes(^reader, 4);
+        ty0 = io.read_i32(^reader);
+        io.skip_bytes(^reader, 2);
+        ty1 = io.read_i32(^reader);
+    }
+
+    max := 0;
+    count := 0;
+
+    for dx: 1 .. 1000 {
+        for dy: -1000 .. 1000 {
+            my, s := simulate(dx, dy);
+            if s {
+                max = math.max(my, max);
+                count += 1;
+            }
+        }
+    }
+
+    printf("Part 1: {}\n", max);
+    printf("Part 2: {}\n", count);
+}
diff --git a/tests/aoc-2021/input/day17.txt b/tests/aoc-2021/input/day17.txt
new file mode 100644 (file)
index 0000000..791984a
--- /dev/null
@@ -0,0 +1 @@
+target area: x=79..137, y=-176..-117