From be7eeaa5d32b3b65435713e08a84f1c4fb7514e7 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sun, 3 Dec 2023 09:40:50 +0000 Subject: [PATCH] 2023 Day 3 in a tidy form. --- 2023/puzzle-03-01.cc | 78 ++++++++++++++++++++++++++++++++++++++ 2023/puzzle-03-02.cc | 89 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 2023/puzzle-03-01.cc create mode 100644 2023/puzzle-03-02.cc diff --git a/2023/puzzle-03-01.cc b/2023/puzzle-03-01.cc new file mode 100644 index 0000000..ee3dff3 --- /dev/null +++ b/2023/puzzle-03-01.cc @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; + +constexpr auto is_digit_or_dot(char c) -> bool +{ + return (std::isdigit(c) != 0) || c == '.'; +} + +struct Grid +{ + void push(std::string const& s) + { + if (grid_.empty()) { + grid_.emplace_back(s.length() + 2, '.'); + grid_.emplace_back(s.length() + 2, '.'); + } + + assert(s.length() + 2 == grid_.front().length()); + grid_.emplace(grid_.end() - 1, std::string(".") + s + std::string(".")); + } + + [[nodiscard]] auto sum_parts() const -> UInt + { + UInt sum{0}; + for (auto y{grid_.begin() + 1}; y != grid_.end(); ++y) { + for (auto x{y->begin() + 1}; x != y->end(); ++x) { + if (std::isdigit(*x) != 0) { + bool is_connected{false}; + auto pos = x - y->begin(); + UInt value = 0; + if (!is_digit_or_dot((y - 1)->at(pos - 1))) { is_connected = true; } + if (!is_digit_or_dot((y)->at(pos - 1))) { is_connected = true; } + if (!is_digit_or_dot((y + 1)->at(pos - 1))) { is_connected = true; } + while (std::isdigit(*x) != 0) { + value *= 10; + value += *x - '0'; + if (!is_digit_or_dot((y - 1)->at(pos))) { is_connected = true; } + if (!is_digit_or_dot((y + 1)->at(pos))) { is_connected = true; } + ++x; + ++pos; + } + if (!is_digit_or_dot((y - 1)->at(pos))) { is_connected = true; } + if (!is_digit_or_dot((y)->at(pos))) { is_connected = true; } + if (!is_digit_or_dot((y + 1)->at(pos))) { is_connected = true; } + if (is_connected) { sum += value; } + } + } + } + + return sum; + } + + std::vector grid_; +}; + +auto main() -> int try +{ + Grid grid; + + std::string line; + while (std::getline(std::cin, line)) { + grid.push(line); + } + + std::cout << "Part sum: " << grid.sum_parts() << '\n'; + return EXIT_SUCCESS; +} +catch (...) { + std::cerr << "Uncaught exception.\n"; + return EXIT_FAILURE; +} diff --git a/2023/puzzle-03-02.cc b/2023/puzzle-03-02.cc new file mode 100644 index 0000000..696b0c0 --- /dev/null +++ b/2023/puzzle-03-02.cc @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; + +struct Grid +{ + void push(std::string const& s) + { + if (grid_.empty()) { + grid_.emplace_back(s.length() + 2, '.'); + grid_.emplace_back(s.length() + 2, '.'); + } + + assert(s.length() + 2 == grid_.front().length()); + grid_.emplace(grid_.end() - 1, std::string(".") + s + std::string(".")); + } + + [[nodiscard]] auto loc(std::size_t const y, std::size_t const x) const -> UInt + { + return y * grid_.front().size() + x; + } + + [[nodiscard]] auto sum_parts() const -> UInt + { + std::map> parts; + + for (std::size_t y{1}; y < grid_.size(); ++y) { + for (auto x{1}; x < grid_[y].size(); ++x) { + if (std::isdigit(grid_[y][x]) != 0) { + std::vector stars; + UInt value = 0; + if (grid_[y - 1][x - 1] == '*') { stars.emplace_back(loc(y - 1, x - 1)); } + if (grid_[y][x - 1] == '*') { stars.emplace_back(loc(y, x - 1)); } + if (grid_[y + 1][x - 1] == '*') { stars.emplace_back(loc(y + 1, x - 1)); } + while (std::isdigit(grid_[y][x]) != 0) { + value *= 10; + value += grid_[y][x] - '0'; + if (grid_[y - 1][x] == '*') { stars.emplace_back(loc(y - 1, x)); } + if (grid_[y + 1][x] == '*') { stars.emplace_back(loc(y + 1, x)); } + ++x; + } + if (grid_[y - 1][x] == '*') { stars.emplace_back(loc(y - 1, x)); } + if (grid_[y][x] == '*') { stars.emplace_back(loc(y, x)); } + if (grid_[y + 1][x] == '*') { stars.emplace_back(loc(y + 1, x)); } + + for (auto star : stars) { + auto [it, inserted] = parts.insert(std::make_pair(star, std::vector())); + it->second.push_back(value); + } + } + } + } + + UInt sum{0}; + for (auto const& [id, sums] : parts) { + if (sums.size() == 2) { + sum += sums[0] * sums[1]; + } + } + + return sum; + } + + std::vector grid_; +}; + +auto main() -> int try { + Grid grid; + + std::string line; + while (std::getline(std::cin, line)) { + grid.push(line); + } + + std::cout << "Part sum: " << grid.sum_parts() << '\n'; + return EXIT_SUCCESS; +} +catch (...) { + std::cerr << "Uncaught exception.\n"; + return EXIT_FAILURE; +}