From 5150b53c48436b846543d78533a93f301560ad80 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sat, 24 Dec 2022 17:20:43 +0000 Subject: [PATCH] Solutions for 2018 day 14 --- 2018/puzzle-14-01.cc | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2018/puzzle-14-02.cc | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 2018/puzzle-14-01.cc create mode 100644 2018/puzzle-14-02.cc diff --git a/2018/puzzle-14-01.cc b/2018/puzzle-14-01.cc new file mode 100644 index 0000000..6655d59 --- /dev/null +++ b/2018/puzzle-14-01.cc @@ -0,0 +1,47 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include +#include + +using Int = std::int64_t; +using UInt = std::uint64_t; +using Point = std::pair; + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line.\n"; + return EXIT_FAILURE; + } + UInt const rounds{std::stoull(line)}; + + std::vector scores; + scores.push_back(3); + scores.push_back(7); + UInt elf1_pos{0}; + UInt elf2_pos{1}; + + while (scores.size() < rounds + 10) { + uint8_t const new_score{static_cast(scores[elf1_pos] + scores[elf2_pos])}; + assert(new_score < 100); + if (new_score >= 10) { + scores.push_back(new_score / 10); + } + scores.push_back(new_score % 10); + elf1_pos = (elf1_pos + 1 + scores[elf1_pos]) % scores.size(); + elf2_pos = (elf2_pos + 1 + scores[elf2_pos]) % scores.size(); + } + + std::cout << "Score: "; + for (UInt idx{rounds}; idx < rounds + 10; ++idx) { + std::cout << static_cast(scores[idx]); + } + std::cout << "\n"; +} diff --git a/2018/puzzle-14-02.cc b/2018/puzzle-14-02.cc new file mode 100644 index 0000000..14053ee --- /dev/null +++ b/2018/puzzle-14-02.cc @@ -0,0 +1,47 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include +#include + +using Int = std::int64_t; +using UInt = std::uint64_t; +using Point = std::pair; + +auto main() -> int +{ + std::string search; + if (!std::getline(std::cin, search)) { + std::cerr << "Unable to read line.\n"; + return EXIT_FAILURE; + } + std::string scores{"37"}; + UInt elf1_pos{0}; + UInt elf2_pos{1}; + + UInt rounds{1'000'000}; + while (true) { + while (scores.size() < rounds) { + Int const new_score{scores[elf1_pos] - '0' + scores[elf2_pos] - '0'}; + assert(new_score < 100); + if (new_score >= 10) { + scores.push_back(static_cast('0' + (new_score / 10))); + } + scores.push_back(static_cast('0' + (new_score % 10))); + elf1_pos = (elf1_pos + 1 + scores[elf1_pos] - '0') % scores.size(); + elf2_pos = (elf2_pos + 1 + scores[elf2_pos] - '0') % scores.size(); + } + rounds += 1'000'000; + + auto it{scores.find(search)}; + if (it != std::string::npos) { + std::cout << "Search string appears after: " << it << "\n"; + return EXIT_SUCCESS; + } + } +}