From d4f024130bb6b4708ed7c4d8a4e80b9b75a4778d Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 17 Dec 2021 09:21:37 -0600 Subject: [PATCH] added day 17 of aoc-2021 --- tests/aoc-2021/day17 | 2 ++ tests/aoc-2021/day17.onyx | 61 ++++++++++++++++++++++++++++++++++ tests/aoc-2021/input/day17.txt | 1 + 3 files changed, 64 insertions(+) create mode 100644 tests/aoc-2021/day17 create mode 100644 tests/aoc-2021/day17.onyx create mode 100644 tests/aoc-2021/input/day17.txt diff --git a/tests/aoc-2021/day17 b/tests/aoc-2021/day17 new file mode 100644 index 00000000..b2df5172 --- /dev/null +++ b/tests/aoc-2021/day17 @@ -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 index 00000000..03d9f671 --- /dev/null +++ b/tests/aoc-2021/day17.onyx @@ -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 index 00000000..791984af --- /dev/null +++ b/tests/aoc-2021/input/day17.txt @@ -0,0 +1 @@ +target area: x=79..137, y=-176..-117 -- 2.25.1