From f6234c926b911f4fb44498ab58529d8e85807457 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Wed, 2 Feb 2022 22:42:20 +0100 Subject: day17 --- 2021/day17/day17 | Bin 0 -> 99088 bytes 2021/day17/day17.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2021/day17/makefile | 2 ++ 3 files changed, 89 insertions(+) create mode 100755 2021/day17/day17 create mode 100644 2021/day17/day17.cpp create mode 100644 2021/day17/makefile diff --git a/2021/day17/day17 b/2021/day17/day17 new file mode 100755 index 0000000..d941d78 Binary files /dev/null and b/2021/day17/day17 differ diff --git a/2021/day17/day17.cpp b/2021/day17/day17.cpp new file mode 100644 index 0000000..305900a --- /dev/null +++ b/2021/day17/day17.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +// -- notes +// if vy(0) > 0 the trajectory always reaches y=0 at time t +// and vy(t) = -vy(0), vy(t+1) = -vy(0)-1 +// sum {i:1..n} (n) = ((n)(n+1))/2 +// triangle = { i in N | exist an n in N s.t. i = sum(n) } +// if probe reaches y=0 with vx(t)=0, vx(0) is in triangle +// so for every vx(0) in sumn and sum(vx(0)) in target +// get the largest vy(t) in target +// this approach only works if vy(0)>0 + +std::vector parse_range(std::string raw) { + std::vector range; + std::string nolabel = raw.substr(2); + auto dotdot = nolabel.find(".."); + std::string r = nolabel.substr(0, dotdot); + std::string l = nolabel.substr(dotdot+2); + range.push_back(std::stoi(r)); + range.push_back(std::stoi(l)); + return range; +} + +int get_trig (int n) { + return ((n)*(n+1))/2; +} + +int validate (int vx, int vy, std::vector range, int maxt) { + int x = 0, y = 0; + for (int t=0; t 0) vx--; + vy--; + if (range[0] <= x && x <= range[1]) + if (range[2] <= y && y <= range[3]) + return 1; + if (y < range[2]) return 0; + } + return 0; +} + +int main (int argc, char *argv[]) { + std::string raw = std::string({ argv[1] }); + std::string nolabel = raw.substr(13); + auto comma = nolabel.find(", "); + std::string rangex = nolabel.substr(0, comma); + std::string rangey = nolabel.substr(comma+2); + auto range = parse_range(rangex); + auto ry = parse_range(rangey); + range.insert(std::end(range), std::begin(ry), std::end(ry)); + // range is x1, x2, y1, y2 + + std::map triangles {}; + for (int i=0; i<100; i++) { + triangles[get_trig(i)] = i; + } + + int vx = 0; + int vy = 0; + for (int i=range[0]; i vy) { + vy = -j-1; + vx = triangles[i]; + } + } + } + } + + std::cout << "velocity: vx=" << vx << ", vy=" << vy << std::endl; + std::cout << "max height: " << get_trig(vy) << std::endl; + + int sum = 0; + for (int i=0; i