From 0a41c0529d27d2604f0ee23f49dcc076207738f6 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Fri, 15 Dec 2023 08:48:52 +0000 Subject: [PATCH] 2023 Day 15 --- 2023/puzzle-15-01.cc | 45 +++++++++++++++++++++++++ 2023/puzzle-15-02.cc | 79 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 2023/puzzle-15-01.cc create mode 100644 2023/puzzle-15-02.cc diff --git a/2023/puzzle-15-01.cc b/2023/puzzle-15-01.cc new file mode 100644 index 0000000..9bfba73 --- /dev/null +++ b/2023/puzzle-15-01.cc @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; + +auto hash(std::string_view const str) -> std::uint8_t +{ + std::uint8_t hash{0}; + for (auto const c : str) { + hash += static_cast(c); + hash += hash << 4; + } + + return hash; +} + +auto main() -> int try { + std::string line; + UInt total_hash{0}; + + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read input."; + } + + std::size_t pos{0}; + while (pos < line.size()) { + auto next_pos = line.find(',', pos); + if (next_pos == std::string::npos) { next_pos = line.size(); } + auto const h = hash(line.substr(pos, next_pos - pos)); + std::cout << line.substr(pos, next_pos - pos) << ": " << static_cast(h) << '\n'; + total_hash += h; + pos = next_pos + 1; + } + + std::cout << "Total: " << total_hash << '\n'; + + return EXIT_SUCCESS; +} +catch (...) { + std::cerr << "Uncaught exception.\n"; + return EXIT_FAILURE; +} diff --git a/2023/puzzle-15-02.cc b/2023/puzzle-15-02.cc new file mode 100644 index 0000000..97c8dc8 --- /dev/null +++ b/2023/puzzle-15-02.cc @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; +using Lens = std::pair; +using LensList = std::list; + +auto hash(std::string_view const str) -> std::uint8_t +{ + std::uint8_t hash{0}; + for (auto const c : str) { + hash += static_cast(c); + hash += hash << 4; + } + + return hash; +} + +auto main() -> int try { + std::string line; + std::vector lenses(256); + + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read input."; + } + + for (std::ptrdiff_t pos{0}; pos < line.size(); ++pos) { + auto const start{pos}; + while (line[pos] != '-' && line[pos] != '=') { ++pos; } + std::string_view const id{line.begin() + start, line.begin() + pos}; + auto const h = hash(id); + auto& box = lenses[h]; + auto it = std::ranges::find_if(box, [id](Lens const& l) { + return l.first == id; + }); + if (line[pos] == '-') { + if (it != box.end()) { + box.erase(it); + } + ++pos; + } + else if (line[pos] == '=') { + if (it != box.end()) { + it->second = line[pos + 1] - '0'; + } + else { + box.emplace_back(id, line[pos + 1] - '0'); + } + pos += 2; + } + else { std::abort(); } + } + + UInt total{0}; + UInt box_id{1}; + for (auto const& box : lenses) { + UInt lens_id{1}; + for (auto const [id, focal_length] : box) { + total += box_id * lens_id * focal_length; + ++lens_id; + } + ++box_id; + } + std::cout << "Total: " << total << '\n'; + + return EXIT_SUCCESS; +} +catch (...) { + std::cerr << "Uncaught exception.\n"; + return EXIT_FAILURE; +}