From 8be9c6d37dcec4f884ad36840d9d14be120f9c3e Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Mon, 6 Dec 2021 07:52:06 +0000 Subject: [PATCH] Add 2021 day 6 puzzles --- 2021/puzzle-06-01.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2021/puzzle-06-02.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 2021/puzzle-06-01.cc create mode 100644 2021/puzzle-06-02.cc diff --git a/2021/puzzle-06-01.cc b/2021/puzzle-06-01.cc new file mode 100644 index 0000000..e26bed7 --- /dev/null +++ b/2021/puzzle-06-01.cc @@ -0,0 +1,46 @@ +// +// Created by Matthew Gretton-Dann on 06/12/2021. +// + +#include +#include +#include +#include + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read input.\n"; + return 1; + } + + std::vector fish(9, 0); + for (auto c : line) { + if (c == ',') { + continue; + } + if (c >= '0' && c <= '8') { + ++fish.at(c - '0'); + } + else { + std::cerr << "Can't interpret " << c << '\n'; + return 1; + } + } + + constexpr unsigned days_wanted{80}; + for (unsigned day{0}; day < days_wanted; ++day) { + std::vector new_fish; + std::copy(fish.begin() + 1, fish.end(), std::back_inserter(new_fish)); + new_fish.at(6) += fish.at(0); + new_fish.push_back(fish.at(0)); + std::swap(fish, new_fish); + } + + std::cout << "Fish count after " << days_wanted << " days: " + << std::accumulate(fish.begin(), fish.end(), std::uint64_t{0}, + [](auto a, auto b) { return a + b; }) + << '\n'; + return 0; +} \ No newline at end of file diff --git a/2021/puzzle-06-02.cc b/2021/puzzle-06-02.cc new file mode 100644 index 0000000..be8a346 --- /dev/null +++ b/2021/puzzle-06-02.cc @@ -0,0 +1,46 @@ +// +// Created by Matthew Gretton-Dann on 06/12/2021. +// + +#include +#include +#include +#include + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read input.\n"; + return 1; + } + + std::vector fish(9, 0); + for (auto c : line) { + if (c == ',') { + continue; + } + if (c >= '0' && c <= '8') { + ++fish.at(c - '0'); + } + else { + std::cerr << "Can't interpret " << c << '\n'; + return 1; + } + } + + constexpr unsigned days_wanted{256}; + for (unsigned day{0}; day < days_wanted; ++day) { + std::vector new_fish; + std::copy(fish.begin() + 1, fish.end(), std::back_inserter(new_fish)); + new_fish.at(6) += fish.at(0); + new_fish.push_back(fish.at(0)); + std::swap(fish, new_fish); + } + + std::cout << "Fish count after " << days_wanted << " days: " + << std::accumulate(fish.begin(), fish.end(), std::uint64_t{0}, + [](auto a, auto b) { return a + b; }) + << '\n'; + return 0; +} \ No newline at end of file