From 300456b9b2b7377bf54182b889fc9d01427bd15e Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sat, 11 Dec 2021 07:36:13 +0000 Subject: [PATCH] Add 2021 day 11 puzzles --- 2021/puzzle-11-01.cc | 83 +++++++++++++++++++++++++++++++++++++++++++ 2021/puzzle-11-02.cc | 84 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 2021/puzzle-11-01.cc create mode 100644 2021/puzzle-11-02.cc diff --git a/2021/puzzle-11-01.cc b/2021/puzzle-11-01.cc new file mode 100644 index 0000000..f58cff4 --- /dev/null +++ b/2021/puzzle-11-01.cc @@ -0,0 +1,83 @@ +// +// Created by Matthew Gretton-Dann on 11/12/2021. +// + +#include +#include +#include +#include +#include + +using Coord = std::pair; + +struct State +{ + State(std::vector const& initial) + { + for (int y = 0; y < initial.size(); ++y) { + for (int x = 0; x < initial[y].size(); ++x) { + octopi_.insert({{x, y}, initial[y][x] - '0'}); + } + } + } + + unsigned sparkle() + { + std::set visited; + // Start by incrementing every octopus by one. + for (auto& o : octopi_) { + (o.second)++; + } + + auto just_visited{0}; + do { + just_visited = visited.size(); + for (auto const& o : octopi_) { + if (o.second > 9 && !visited.contains(o.first)) { + visited.insert({o.first}); + for (int x = o.first.first - 1; x < o.first.first + 2; ++x) { + for (int y = o.first.second - 1; y < o.first.second + 2; ++y) { + auto it{octopi_.find({x, y})}; + if (it != octopi_.end()) { + ++(it->second); + } + } + } + } + } + } while (just_visited != visited.size()); + + // And reset the energy levels + for (auto& o : octopi_) { + if (o.second > 9) { + o.second = 0; + } + } + + return visited.size(); + } + +private: + std::map octopi_; +}; + +auto main() -> int +{ + std::string line; + std::vector lines; + + while (std::getline(std::cin, line)) { + lines.push_back(line); + } + + State octopi(lines); + + constexpr unsigned max_steps{100}; + unsigned count{0}; + for (unsigned i = 0; i < max_steps; ++i) { + count += octopi.sparkle(); + } + + std::cout << "After " << max_steps << " we have sparkled " << count << " times.\n"; + return 0; +} diff --git a/2021/puzzle-11-02.cc b/2021/puzzle-11-02.cc new file mode 100644 index 0000000..e1d42bd --- /dev/null +++ b/2021/puzzle-11-02.cc @@ -0,0 +1,84 @@ +// +// Created by Matthew Gretton-Dann on 11/12/2021. +// + +#include +#include +#include +#include +#include + +using Coord = std::pair; + +struct State +{ + State(std::vector const& initial) + { + for (int y = 0; y < initial.size(); ++y) { + for (int x = 0; x < initial[y].size(); ++x) { + octopi_.insert({{x, y}, initial[y][x] - '0'}); + } + } + } + + unsigned sparkle() + { + std::set visited; + // Start by incrementing every octopus by one. + for (auto& o : octopi_) { + (o.second)++; + } + + auto just_visited{0}; + do { + just_visited = visited.size(); + for (auto const& o : octopi_) { + if (o.second > 9 && !visited.contains(o.first)) { + visited.insert({o.first}); + for (int x = o.first.first - 1; x < o.first.first + 2; ++x) { + for (int y = o.first.second - 1; y < o.first.second + 2; ++y) { + auto it{octopi_.find({x, y})}; + if (it != octopi_.end()) { + ++(it->second); + } + } + } + } + } + } while (just_visited != visited.size()); + + // And reset the energy levels + for (auto& o : octopi_) { + if (o.second > 9) { + o.second = 0; + } + } + + return visited.size(); + } + +private: + std::map octopi_; +}; + +auto main() -> int +{ + std::string line; + std::vector lines; + + unsigned grid_size{0}; + while (std::getline(std::cin, line)) { + grid_size += line.size(); + lines.push_back(line); + } + + State octopi(lines); + unsigned step{0}; + + do { + ++step; + } while (octopi.sparkle() != grid_size); + + std::cout << "Synchronization after " << step << " steps.\n"; + return 0; +}