From 5b3cfac3c45e4b1aa6cd95dfc6fd1bf0a6ab7ecd Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Wed, 13 Dec 2023 09:06:19 +0000 Subject: [PATCH] 2023 Day 13 Part 1 And optimized. --- 2023/puzzle-13-01.cc | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 2023/puzzle-13-01.cc diff --git a/2023/puzzle-13-01.cc b/2023/puzzle-13-01.cc new file mode 100644 index 0000000..e26cd99 --- /dev/null +++ b/2023/puzzle-13-01.cc @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; + +struct Grid +{ + void clear() { rows_.clear(); } + void push_back(std::string const& row) { rows_.push_back(row); } + + [[nodiscard]] auto reflection_point() -> UInt + { + for (std::size_t row{0}; row < rows_.size() - 1; ++row) { + auto top{row}; + auto bottom{row + 1}; + while (rows_[top] == rows_[bottom]) { + if (top == 0 || bottom == rows_.size() - 1) { return row + 1; } + --top; + ++bottom; + } + } + + return 0; + } + + [[nodiscard]] auto rotate_right() const -> Grid + { + Grid new_grid; + new_grid.rows_.resize(rows_.front().size()); + for (auto row{rows_.rbegin()}; row != rows_.rend(); ++row) { + for (std::size_t i{0}; i < row->size(); ++i) { + new_grid.rows_[i].push_back(row->at(i)); + } + } + + return new_grid; + } + +private: + std::vector rows_; +}; + +auto main() -> int try { + std::string line; + + Grid grid; + UInt rows{0}; + UInt cols{0}; + while (std::getline(std::cin, line)) { + if (line.empty()) { + rows += grid.reflection_point(); + grid = grid.rotate_right(); + cols += grid.reflection_point(); + grid.clear(); + } + else { grid.push_back(line); } + } + + rows += grid.reflection_point(); + grid = grid.rotate_right(); + cols += grid.reflection_point(); + + std::cout << "Columns to left: " << cols << '\n' << "Rows above: " << rows << '\n' << "Score: " << + cols + (rows * 100) << '\n'; + + return EXIT_SUCCESS; +} +catch (...) { + std::cerr << "Uncaught exception.\n"; + return EXIT_FAILURE; +}