From 44d79ff3efaca242532e142f3796328918c1e942 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sun, 29 Dec 2024 14:48:07 -0600 Subject: [PATCH] 2024-19 --- 2024/19.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2024/CMakeLists.txt | 3 +++ 2 files changed, 65 insertions(+) create mode 100644 2024/19.cpp diff --git a/2024/19.cpp b/2024/19.cpp new file mode 100644 index 0000000..39b3d17 --- /dev/null +++ b/2024/19.cpp @@ -0,0 +1,62 @@ +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace qi = boost::spirit::qi; + +using std::istream; +using std::ostream; +using std::size_t; +using std::string_view; +using std::string; +using std::tuple; +using std::uint64_t; +using std::vector; + +namespace { + +auto ways(vector const& towels, string const& design) -> uint64_t +{ + vector counts(design.size() + 1); + counts[0] = 1; + + for (size_t i = 0; i < design.size(); i++) { + auto const suffix = string_view{ begin(design) + i, end(design) }; + for (auto && towel : towels) { + if (suffix.starts_with(towel)) { + counts[i + towel.size()] += counts[i]; + } + } + } + + return counts.back(); +} + +} // namespace + +auto Main(istream & in, ostream & out) -> void +{ + auto const [towels, designs] = + aocpp::ParseSimple + , vector>> + (in, +qi::alpha % ", " >> "\n\n" >> *(+qi::alpha >> "\n")); + + uint64_t p1 = 0, p2 = 0; + for (auto && d : designs) { + auto const n = ways(towels, d); + if (n > 0) { p1++; } + p2 += n; + } + + out << "Part 1: " << p1 << "\n" + << "Part 2: " << p2 << "\n"; +} diff --git a/2024/CMakeLists.txt b/2024/CMakeLists.txt index e4d52ec..866866d 100644 --- a/2024/CMakeLists.txt +++ b/2024/CMakeLists.txt @@ -4,5 +4,8 @@ target_link_libraries(2024_01 aocpp Boost::headers) add_executable(2024_07 07.cpp) target_link_libraries(2024_07 aocpp Boost::headers) +add_executable(2024_19 19.cpp) +target_link_libraries(2024_19 aocpp Boost::headers) + add_executable(2024_22 22.cpp) target_link_libraries(2024_22 aocpp)