From 9f7b0cd76272068588d0a7220408f91390c5b583 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Thu, 6 Jan 2022 18:42:22 +0100 Subject: moved --- 2021/day07/day07 | Bin 0 -> 76312 bytes 2021/day07/day07.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2021/day07/day07_input.txt | 1 + 2021/day07/day07_input_ez.txt | 1 + 2021/day07/makefile | 2 ++ 5 files changed, 44 insertions(+) create mode 100755 2021/day07/day07 create mode 100644 2021/day07/day07.cpp create mode 100644 2021/day07/day07_input.txt create mode 100644 2021/day07/day07_input_ez.txt create mode 100644 2021/day07/makefile (limited to '2021/day07') diff --git a/2021/day07/day07 b/2021/day07/day07 new file mode 100755 index 0000000..4cbc735 Binary files /dev/null and b/2021/day07/day07 differ diff --git a/2021/day07/day07.cpp b/2021/day07/day07.cpp new file mode 100644 index 0000000..f9bbd1a --- /dev/null +++ b/2021/day07/day07.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +#include "../utils.h" + +long sumdist (std::vector pos, int pole, int inc) { + long sd = 0; + for (auto p : pos) { + int n = abs(p-pole); + if (inc == 1) n = (n * (n+1)) / 2; + sd += n; + } + return sd; +} + +int main (int argc, char *argv[]) { + std::string raw; + std::getline(std::ifstream(argv[1]), raw, '\0'); + std::vector vec; + split(vec, raw, ","); + std::vector pos; + for (auto s : vec) pos.push_back(std::stoi(s)); + + int minp = 10000, maxp = -10000; + for (auto p : pos) { + minp = std::min(minp, p); + maxp = std::max(maxp, p); + } + int inc = argc > 2 ? atoi(argv[2]) : 0; + long fuel = 100000000000; + for (int i=minp; i