diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-06 18:42:22 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-06 18:42:22 +0100 |
commit | 9f7b0cd76272068588d0a7220408f91390c5b583 (patch) | |
tree | 49a517f0303c951023554ba6b3fb8cc3d5eb1234 /2021/day06/day06.cpp | |
parent | 72a3388c042f4812e2db33f6d6a1b757392a18a6 (diff) |
moved
Diffstat (limited to '2021/day06/day06.cpp')
-rw-r--r-- | 2021/day06/day06.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/2021/day06/day06.cpp b/2021/day06/day06.cpp new file mode 100644 index 0000000..38d602b --- /dev/null +++ b/2021/day06/day06.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include <fstream> +#include <string> +#include <vector> + +#include "../utils.h" + +long sum (std::vector<long> vec) { + long s = 0; for (long v : vec) s += v; return s; +} + +int main (int argc, char *argv[]) { + std::string raw; + std::getline(std::ifstream(argv[1]), raw, '\0'); + std::vector<std::string> strfish; + split(strfish, raw, ","); + + std::vector<long> fish; + for (int i=0; i<9; i++) { fish.push_back(0); } + for (std::string str : strfish) fish[std::stoi(str)] += 1; + + int days = 80; + if (argc > 2) days = std::stoi(std::string(argv[2])); + for (int i=0; i<days; i++) { + long born = fish[0]; + fish[0] = 0; + for (int j=1; j<9; j++) { + fish[j-1] += fish[j]; + fish[j] = 0; + } + fish[6] += born; + fish[8] = born; + + if (argc > 3 && std::string(argv[3]) == "-w") { + std::cout << "day " << i+1 << "/" << days + << " current fish: " << sum(fish) << std::endl; + } + if (argc > 3 && std::string(argv[3]) == "-v") { + std::cout << "day " << i+1 << ": "; + for (auto f : fish) std::cout << f << ","; + std::cout << std::endl; + } + } + + std::cout << "fish: " << sum(fish) << std::endl; + return 0; +} |