From b1ba25890e629e51fdddf41fd981d710ffbc753a Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Wed, 21 Dec 2022 13:20:03 +0000 Subject: [PATCH] Solutions for 2018 day 8 --- 2018/puzzle-08-01.cc | 61 +++++++++++++++++++++++++++++++++++++ 2018/puzzle-08-02.cc | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 2018/puzzle-08-01.cc create mode 100644 2018/puzzle-08-02.cc diff --git a/2018/puzzle-08-01.cc b/2018/puzzle-08-01.cc new file mode 100644 index 0000000..de4962d --- /dev/null +++ b/2018/puzzle-08-01.cc @@ -0,0 +1,61 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include +#include +#include +#include + +using Int = std::int64_t; + +template +auto read_int(iterator& begin, iterator end) -> Int +{ + while (begin != end && *begin == ' ') { + ++begin; + } + Int result{0}; + while (begin != end && std::isdigit(*begin) != 0) { + result *= 10; + result += *begin - '0'; + ++begin; + } + return result; +} + +template +auto process_string(iterator& begin, iterator end) -> Int +{ + Int score{0}; + Int child_count{read_int(begin, end)}; + Int metadata_count{read_int(begin, end)}; + for (Int i{0}; i < child_count; ++i) { + score += process_string(begin, end); + } + for (Int i{0}; i < metadata_count; ++i) { + score += read_int(begin, end); + } + return score; +} + +auto main() -> int +{ + std::string line; + + // Read data + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line.\n"; + return EXIT_FAILURE; + } + + auto begin{line.begin()}; + Int sum{process_string(begin, line.end())}; + std::cout << "Sum: " << sum << "\n"; + + return EXIT_SUCCESS; +} diff --git a/2018/puzzle-08-02.cc b/2018/puzzle-08-02.cc new file mode 100644 index 0000000..f1dd838 --- /dev/null +++ b/2018/puzzle-08-02.cc @@ -0,0 +1,72 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include +#include +#include +#include + +using Int = std::int64_t; + +template +auto read_int(iterator& begin, iterator end) -> Int +{ + while (begin != end && *begin == ' ') { + ++begin; + } + Int result{0}; + while (begin != end && std::isdigit(*begin) != 0) { + result *= 10; + result += *begin - '0'; + ++begin; + } + return result; +} + +template +auto process_string(iterator& begin, iterator end) -> Int +{ + Int child_count{read_int(begin, end)}; + Int metadata_count{read_int(begin, end)}; + std::vector child_scores; + child_scores.push_back(0); + for (Int i{0}; i < child_count; ++i) { + child_scores.push_back(process_string(begin, end)); + } + + Int score{0}; + for (Int i{0}; i < metadata_count; ++i) { + if (child_count > 0) { + Int idx{read_int(begin, end)}; + if (idx < child_scores.size()) { + score += child_scores[idx]; + } + } + else { + score += read_int(begin, end); + } + } + return score; +} + +auto main() -> int +{ + std::string line; + + // Read data + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line.\n"; + return EXIT_FAILURE; + } + + auto begin{line.begin()}; + Int sum{process_string(begin, line.end())}; + std::cout << "Sum: " << sum << "\n"; + + return EXIT_SUCCESS; +}