diff --git a/res/2025/day11/test_input.txt b/res/2025/day11/test_input.txt new file mode 100644 index 0000000..d787665 --- /dev/null +++ b/res/2025/day11/test_input.txt @@ -0,0 +1,13 @@ +svr: aaa bbb +aaa: fft +fft: ccc +bbb: tty +tty: ccc +ccc: ddd eee +ddd: hub +hub: fff +eee: dac +dac: fff +fff: ggg hhh +ggg: out +hhh: out diff --git a/src/2025/Day11.cpp b/src/2025/Day11.cpp index 61606d3..97411fb 100644 --- a/src/2025/Day11.cpp +++ b/src/2025/Day11.cpp @@ -2,35 +2,100 @@ namespace y2025::day11 { - using InputType = std::vector; - REGISTER_DAY(2025, Day11, InputType, int); + using InputType = std::map>; + REGISTER_DAY(2025, Day11, InputType, int64_t); - REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 1, 0); - REGISTER_TEST(2025, Day11, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 2, 0); - REGISTER_TEST(2025, Day11, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 1, 8); + REGISTER_TEST(2025, Day11, Input, 1, 613); + REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 2, 2); + REGISTER_TEST(2025, Day11, Input, 2, 372918445876116); READ_INPUT(input) { - std::vector vec; + std::map> vec; std::string str; while (std::getline(input, str)) { std::stringstream ss{str}; int n; - ss >> n; - vec.emplace_back(n); + std::string str2; + ss >> str2; + str2.pop_back(); + std::string str3; + + std::vector paths; + while (ss >> str3) + { + paths.emplace_back(str3); + } + vec[str2] = paths; } return vec; } + struct State + { + std::string current; + bool fft; + bool dac; + + bool operator<(const State& other) const + { + if (fft != other.fft) + return fft < other.fft; + if (dac != other.dac) + return dac < other.dac; + return current < other.current; + } + }; + + int PathFind(const std::map>& paths, const std::string& current, int count) + { + if (current == "out") + return 1; + + int validPaths = 0; + for (auto& branch : paths.at(current)) + { + validPaths += PathFind(paths, branch, count + 1); + } + return validPaths; + } + + int64_t PathFind2(const std::map>& paths, + const State& currentState, + std::map& memoization) + { + if (currentState.current == "out") + { + if (currentState.fft && currentState.dac) + return 1; + return 0; + } + + auto memIt = memoization.find(currentState); + if (memIt != memoization.end()) + return memIt->second; + + int64_t validPaths = 0; + for (auto& branch : paths.at(currentState.current)) + { + bool fft = currentState.fft || branch == "fft"; + bool dac = currentState.dac || branch == "dac"; + validPaths += PathFind2(paths, State{branch, fft, dac}, memoization); + } + memoization[currentState] = validPaths; + return validPaths; + } + OUTPUT1(input) { - return 0; + return PathFind(input, isExample ? "svr" : "you", 0); } OUTPUT2(input) { - return 0; + std::map memoization{}; + return PathFind2(input, State{"svr", false, false}, memoization); } }