aboutsummaryrefslogtreecommitdiff
path: root/2021/day07/day07.cpp
diff options
context:
space:
mode:
authorjacopograndi <jacopo.grandi@outlook.it>2022-01-06 18:42:22 +0100
committerjacopograndi <jacopo.grandi@outlook.it>2022-01-06 18:42:22 +0100
commit9f7b0cd76272068588d0a7220408f91390c5b583 (patch)
tree49a517f0303c951023554ba6b3fb8cc3d5eb1234 /2021/day07/day07.cpp
parent72a3388c042f4812e2db33f6d6a1b757392a18a6 (diff)
moved
Diffstat (limited to '2021/day07/day07.cpp')
-rw-r--r--2021/day07/day07.cpp40
1 files changed, 40 insertions, 0 deletions
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 <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+
+#include "../utils.h"
+
+long sumdist (std::vector<int> 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<std::string> vec;
+ split(vec, raw, ",");
+ std::vector<int> 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<maxp; i++) {
+ fuel = std::min(fuel, sumdist(pos, i, inc));
+ }
+
+ std::cout << "fuel: " << fuel << std::endl;
+
+ return 0;
+}