This commit is contained in:
Eric Mertens 2024-12-29 15:13:26 -06:00
parent 44d79ff3ef
commit d3a6fda730

View File

@ -6,8 +6,8 @@
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
#include <string_view>
#include <string> #include <string>
#include <string_view>
#include <tuple> #include <tuple>
#include <vector> #include <vector>
@ -16,44 +16,53 @@ namespace qi = boost::spirit::qi;
using std::istream; using std::istream;
using std::ostream; using std::ostream;
using std::size_t; using std::size_t;
using std::string_view;
using std::string; using std::string;
using std::string_view;
using std::tuple; using std::tuple;
using std::uint64_t; using std::uint64_t;
using std::vector; using std::vector;
namespace { namespace
auto ways(vector<string> const& towels, string const& design) -> uint64_t
{ {
vector<uint64_t> counts(design.size() + 1);
counts[0] = 1;
for (size_t i = 0; i < design.size(); i++) { auto ways(const vector<string> &towels, const string &design) -> uint64_t
auto const suffix = string_view{ begin(design) + i, end(design) }; {
for (auto && towel : towels) { const auto n = design.size();
if (suffix.starts_with(towel)) { vector<uint64_t> counts(n + 1);
counts[i + towel.size()] += counts[i]; counts[0] = 1;
for (size_t i = 0; i < n; i++)
{
string_view const suffix{begin(design) + i, end(design)};
for (auto &&towel : towels)
{
if (suffix.starts_with(towel))
{
counts[i + towel.size()] += counts[i];
}
} }
} }
}
return counts.back(); return counts.back();
} }
} // namespace } // namespace
auto Main(istream & in, ostream & out) -> void auto Main(istream &in, ostream &out) -> void
{ {
auto const [towels, designs] = const auto [towels, designs] =
aocpp::ParseSimple aocpp::ParseSimple
<tuple<vector<string>, vector<string>>> <tuple<vector<string>, vector<string>>>
(in, +qi::alpha % ", " >> "\n\n" >> *(+qi::alpha >> "\n")); (in, +qi::alpha % ", " >> "\n\n" >> *(+qi::alpha >> "\n"));
uint64_t p1 = 0, p2 = 0; uint64_t p1 = 0, p2 = 0;
for (auto && d : designs) { for (auto &&design : designs)
auto const n = ways(towels, d); {
if (n > 0) { p1++; } const auto n = ways(towels, design);
if (n > 0)
{
p1++;
}
p2 += n; p2 += n;
} }