63 lines
1.3 KiB
C++
63 lines
1.3 KiB
C++
#include <aocpp/Parsing.hpp>
|
|
#include <aocpp/Startup.hpp>
|
|
|
|
#include <boost/spirit/include/qi.hpp>
|
|
#include <boost/fusion/include/std_tuple.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <string_view>
|
|
#include <string>
|
|
#include <tuple>
|
|
#include <vector>
|
|
|
|
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<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 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
|
|
<tuple<vector<string>, vector<string>>>
|
|
(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";
|
|
}
|