From 680e29b45dde4261eed8a0cde7949f1f90962a78 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 19 Dec 2024 17:56:24 +0100 Subject: aoc 2024, day 19, alternative Lösung MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2024/src/day19-alt.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ 2024/src/day19.cpp | 7 +--- 2 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 2024/src/day19-alt.cpp diff --git a/2024/src/day19-alt.cpp b/2024/src/day19-alt.cpp new file mode 100644 index 0000000..77c03db --- /dev/null +++ b/2024/src/day19-alt.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector +split(string_view line, string_view delimiter) +{ + size_t pos_start = 0; + size_t pos_end = 0; + + vector res; + + while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) { + auto token = line.substr(pos_start, pos_end - pos_start); + pos_start = pos_end + delimiter.length(); + + res.emplace_back(token); + } + + res.emplace_back(line.substr(pos_start)); + return res; +} + +tuple, vector> +read_file(string_view filename) +{ + fstream input{ filename }; + + vector lines; + for ( string line; getline(input, line); ) { + lines.emplace_back(line); + } + + const auto parts = split(lines[0], ", "); + + return { { parts.begin(), parts.end() }, { lines.begin() + 2, lines.end() } }; +} + +void +part1(const tuple, vector>& data) +{ + const auto& parts = get<0>(data); + const auto& lines = get<1>(data); + + map cache; + + function match = [&](const string& line) { + if ( line.empty() ) { + return true; + } + + if ( cache.contains(line) ) { + return cache.at(line); + } + + for ( const auto& part: parts ) { + if ( line.starts_with(part) ) { + if ( match(line.substr(part.size())) ) { + return cache[line] = true; + } + } + } + + return cache[line] = false; + }; + + cout << ranges::count_if(lines, match) << endl; +} + +void +part2(const tuple, vector>& data) +{ + const auto& parts = get<0>(data); + const auto& lines = get<1>(data); + + map cache; + + function match = [&](const string& line) -> long { + if ( line.empty() ) { + return 1; + } + + if ( cache.contains(line) ) { + return cache.at(line); + } + + long counter = 0; + for ( const auto& part: parts ) { + if ( line.starts_with(part) ) { + counter += match(line.substr(part.size())); + } + } + + return cache[line] = counter; + }; + + cout << accumulate(lines.begin(), lines.end(), 0L, [&](auto init, const auto& line) { return init + match(line); }) << endl; +} + +int +main() +{ + const auto data = read_file("data/day19.txt"); + part1(data); + part2(data); +} diff --git a/2024/src/day19.cpp b/2024/src/day19.cpp index 43995b1..c114d2a 100644 --- a/2024/src/day19.cpp +++ b/2024/src/day19.cpp @@ -92,11 +92,8 @@ part2(const tuple, vector>& data) long counter = 0; for ( size_t i = 0; i != line.size(); ++i ) { - auto lhs = line.substr(0, i + 1); - auto rhs = line.substr(i + 1); - - if ( parts.contains(lhs) ) { - counter += match(rhs); + if ( parts.contains(line.substr(0, i + 1)) ) { + counter += match(line.substr(i + 1)); } } -- cgit v1.3