advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit 81f21c1a2d2f250b13df1ad394465f10bb65132e
parent 812dec89007d8d76f83ff97c88009629a9ff05dc
Author: bsandro <email@bsandro.tech>
Date:   Fri, 20 Dec 2024 00:06:23 +0200

Day 19 p2 naiive approach

Diffstat:
Mday19/main.cpp | 47++++++++++++++++++++---------------------------
1 file changed, 20 insertions(+), 27 deletions(-)

diff --git a/day19/main.cpp b/day19/main.cpp @@ -8,6 +8,12 @@ #include <algorithm> #include "utils.hpp" +class Tree { +public: + std::string root; + std::vector<Tree> branches; +}; + typedef std::pair<std::vector<std::string>, std::vector<std::string>> Data; template<typename T> @@ -42,44 +48,30 @@ T read_file(const std::string &path) { return buf; } -const char * patterns_match(const std::vector<std::string> patterns, const char *s) { - if (*s=='\0') return s; - //std::printf("checking %s...\n", s); +void patterns_match(const std::vector<std::string> patterns, const char *s, int64_t &cnt) { const std::string ss(s); for (const auto &ps:patterns) { if (ss.starts_with(ps)) { const char *ns = s+ps.size(); - //std::printf("%s starts with %s, moving to %s\n", s, ps.c_str(), ns); if (*ns=='\0') { - return ns; + cnt++; } else { - ns = patterns_match(patterns, ns); - if (*ns=='\0') return ns; + patterns_match(patterns, ns, cnt); } } } - //std::printf("shouldn't be here\n"); - return s; } -int64_t part1(Data &input [[ maybe_unused ]]) { - //std::printf("\n"); - int64_t res = 0; +std::pair<int64_t, int64_t> solve(Data &input) { + int64_t r1 = 0; + int64_t r2 = 0; for (const std::string &v : input.second) { - //const std::string &v = input.second[0]; { - //const char *r = patterns_match(input.first, v.c_str()); - //std::printf("........... %s match: %s\n", v.c_str(), r); - if (*patterns_match(input.first, v.c_str())=='\0') { - //std::printf("%s does match\n", v.c_str()); - res++; - //} else { - //std::printf("%s does not match\n", v.c_str()); - } + int64_t cnt = 0; + patterns_match(input.first, v.c_str(), cnt); + if (cnt>0) r1++; + r2+=cnt; } - return res; -} -int64_t part2(Data &input [[ maybe_unused ]]) { - return 0; + return std::make_pair(r1, r2); } int main(int argc, char **argv) { @@ -87,8 +79,9 @@ int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; std::cout << "AoC 2024 day 19 " << fname << std::endl; Data input = read_file<Data>(fname); - std::cout << "part1: " << part1(input) << std::endl; - std::cout << "part2: " << part2(input) << std::endl; + auto [p1, p2] = solve(input); + std::cout << "part1: " << p1 << std::endl; + std::cout << "part2: " << p2 << std::endl; return 0; }