From c6e9727f37409df0d774a49c0ed1496ab00cfad1 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sat, 9 Dec 2023 09:10:17 +0000 Subject: [PATCH] 2023 Day 9 --- 2023/puzzle-09-01.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2023/puzzle-09-02.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 2023/puzzle-09-01.cc create mode 100644 2023/puzzle-09-02.cc diff --git a/2023/puzzle-09-01.cc b/2023/puzzle-09-01.cc new file mode 100644 index 0000000..8c66bd5 --- /dev/null +++ b/2023/puzzle-09-01.cc @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using Int = std::int64_t; +using NumVec = std::vector; + +template +void read_nums(It it, std::string_view const nums) +{ + char const* pos{nums.data()}; + while (pos - nums.data() < nums.size()) { + char* next_pos{nullptr}; + *it++ = std::strtoll(pos, &next_pos, 10); + pos = next_pos; + } +} + +constexpr auto is_zero(Int const num) -> bool { return num == 0; } + +auto main() -> int try { + std::string line; + Int sum{0}; + + while (std::getline(std::cin, line)) { + std::stack diffs; + diffs.emplace(); + read_nums(std::back_inserter(diffs.top()), line); + + while (!std::all_of(diffs.top().begin(), diffs.top().end(), is_zero)) { + NumVec diff; + auto const& top = diffs.top(); + for (auto it = top.begin() + 1; it != top.end(); ++it) { + diff.emplace_back(*it - *(it - 1)); + } + diffs.push(diff); + } + + Int next{0}; + while (!diffs.empty()) { + auto const& top = diffs.top(); + next += top.back(); + diffs.pop(); + } + + std::cout << line << ": " << next << '\n'; + sum += next; + } + + std::cout << "Sum: " << sum; + + return EXIT_SUCCESS; +} +catch (...) { + std::cerr << "Uncaught exception.\n"; + return EXIT_FAILURE; +} diff --git a/2023/puzzle-09-02.cc b/2023/puzzle-09-02.cc new file mode 100644 index 0000000..34b48d5 --- /dev/null +++ b/2023/puzzle-09-02.cc @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using Int = std::int64_t; +using NumVec = std::vector; + +template +void read_nums(It it, std::string_view const nums) +{ + char const* pos{nums.data()}; + while (pos - nums.data() < nums.size()) { + char* next_pos{nullptr}; + *it++ = std::strtoll(pos, &next_pos, 10); + pos = next_pos; + } +} + +constexpr auto is_zero(Int const num) -> bool { return num == 0; } + +auto main() -> int try { + std::string line; + Int sum{0}; + + while (std::getline(std::cin, line)) { + std::stack diffs; + diffs.emplace(); + read_nums(std::back_inserter(diffs.top()), line); + + while (!std::all_of(diffs.top().begin(), diffs.top().end(), is_zero)) { + NumVec diff; + auto const& top = diffs.top(); + for (auto it = top.begin() + 1; it != top.end(); ++it) { + diff.emplace_back(*it - *(it - 1)); + } + diffs.push(diff); + } + + Int next{0}; + while (!diffs.empty()) { + auto const& top = diffs.top(); + next = top.front() - next; + diffs.pop(); + } + + std::cout << line << ": " << next << '\n'; + sum += next; + } + + std::cout << "Sum: " << sum; + + return EXIT_SUCCESS; +} +catch (...) { + std::cerr << "Uncaught exception.\n"; + return EXIT_FAILURE; +}