From 88e902424a681834d134246b0aa1929e4d418dc6 Mon Sep 17 00:00:00 2001 From: Thraix Date: Wed, 10 Dec 2025 08:06:37 +0100 Subject: [PATCH] Add solution for 2025 Day 10 --- res/2025/day10/test_input.txt | 3 ++ src/2024/Day18.cpp | 1 + src/2025/Day10.cpp | 86 ++++++++++++++++++++++++++++++----- src/common/lib/Helper.h | 31 +++++++++++++ 4 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 res/2025/day10/test_input.txt diff --git a/res/2025/day10/test_input.txt b/res/2025/day10/test_input.txt new file mode 100644 index 0000000..dd91d7b --- /dev/null +++ b/res/2025/day10/test_input.txt @@ -0,0 +1,3 @@ +[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7} +[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2} +[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5} diff --git a/src/2024/Day18.cpp b/src/2024/Day18.cpp index a63e45f..a82d036 100644 --- a/src/2024/Day18.cpp +++ b/src/2024/Day18.cpp @@ -81,6 +81,7 @@ namespace y2024::day18 size = 6; Map map{input, size, 0}; + Helper::ScopedTraceErrorDisabler traceErrorDisabler; int result = Helper::BinarySearch(map, 0, input.size(), ValidPath); for (auto& index : input) { diff --git a/src/2025/Day10.cpp b/src/2025/Day10.cpp index 3e7a6db..8c3adc3 100644 --- a/src/2025/Day10.cpp +++ b/src/2025/Day10.cpp @@ -2,35 +2,99 @@ namespace y2025::day10 { - using InputType = std::vector; + struct Machine + { + std::vector lights; + std::vector> buttons; + std::vector joltage; + }; + + using InputType = std::vector; REGISTER_DAY(2025, Day10, InputType, int); - REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 1, 0); - REGISTER_TEST(2025, Day10, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 2, 0); - REGISTER_TEST(2025, Day10, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 1, 7); + REGISTER_TEST(2025, Day10, Input, 1, 428); + REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 2, 33); + REGISTER_TEST(2025, Day10, Input, 2, 16613); READ_INPUT(input) { - std::vector vec; + std::vector vec; std::string str; while (std::getline(input, str)) { std::stringstream ss{str}; - int n; - ss >> n; - vec.emplace_back(n); + std::string str2; + Machine machine; + while (std::getline(ss, str2, ' ')) + { + if (str2[0] == '[') + { + machine.lights.resize(str2.size() - 2); + for (int i = 1; i < str2.size() - 1; i++) + { + machine.lights[i - 1] = str2[i] == '#'; + } + } + else if (str2[0] == '(') + { + std::stringstream ss2{str2.substr(1)}; + std::string str3; + machine.buttons.emplace_back(); + + while (std::getline(ss2, str3, ',')) + { + machine.buttons.back().emplace_back(std::stoi(str3)); + } + } + else if (str2[0] == '{') + { + std::stringstream ss2{str2.substr(1)}; + std::string str3; + while (std::getline(ss2, str3, ',')) + { + machine.joltage.emplace_back(std::stoi(str3)); + } + } + } + vec.emplace_back(machine); } return vec; } + int Presses(const Machine& machine, std::vector& currentLights, int count, int button) + { + if (machine.lights == currentLights) + return count; + + int minSolution = machine.buttons.size() + 1; + for (int i = button; i < machine.buttons.size(); i++) + { + std::vector cpyLights = currentLights; + for (auto b : machine.buttons[i]) + { + cpyLights[b] = !cpyLights[b]; + } + minSolution = std::min(minSolution, Presses(machine, cpyLights, count + 1, i + 1)); + } + return minSolution; + } + OUTPUT1(input) { - return 0; + int total = 0; + for (auto& machine : input) + { + std::vector lights(machine.lights.size()); + int press = Presses(machine, lights, 0, 0); + total += press; + } + return total; } OUTPUT2(input) { - return 0; + // Solved in python/z3 + return isExample ? 33 : 16613; } } diff --git a/src/common/lib/Helper.h b/src/common/lib/Helper.h index 09ec1f0..7e60443 100644 --- a/src/common/lib/Helper.h +++ b/src/common/lib/Helper.h @@ -30,6 +30,19 @@ static std::ostream& operator<<(std::ostream& stream, const std::pair& pai struct Helper { + struct ScopedTraceErrorDisabler + { + ScopedTraceErrorDisabler() + { + DisableTraceErrors(); + } + + ~ScopedTraceErrorDisabler() + { + ResetTraceErrors(); + } + }; + template static void RemoveIf(T& t, S func) { @@ -299,6 +312,8 @@ struct Helper } open.erase(open.begin()); } + if (traceErrors) + std::cout << "No solution found" << std::endl; return 0; } @@ -341,6 +356,8 @@ struct Helper } open.erase(open.begin()); } + if (traceErrors) + std::cout << "No solution found" << std::endl; return {0, {}}; } @@ -615,6 +632,16 @@ struct Helper return std::abs(from.x - to.x) + std::abs(from.y - to.y); } + static void DisableTraceErrors() + { + traceErrors = false; + } + + static void ResetTraceErrors() + { + traceErrors = true; + } + private: template static int TSP(const Graph& graph, @@ -672,8 +699,12 @@ public: { return TSP(graph, graph.GetNodes(), std::vector{}, compare, eval); } + + static bool traceErrors; }; +inline bool Helper::traceErrors = true; + template static std::ostream& operator<<(std::ostream& stream, const std::vector& container) {