Add solution for 2025 Day 10

This commit is contained in:
Thraix
2025-12-10 08:06:37 +01:00
parent f5e809d086
commit 88e902424a
4 changed files with 110 additions and 11 deletions
+3
View File
@@ -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}
+1
View File
@@ -81,6 +81,7 @@ namespace y2024::day18
size = 6; size = 6;
Map map{input, size, 0}; Map map{input, size, 0};
Helper::ScopedTraceErrorDisabler traceErrorDisabler;
int result = Helper::BinarySearch(map, 0, input.size(), ValidPath); int result = Helper::BinarySearch(map, 0, input.size(), ValidPath);
for (auto& index : input) for (auto& index : input)
{ {
+75 -11
View File
@@ -2,35 +2,99 @@
namespace y2025::day10 namespace y2025::day10
{ {
using InputType = std::vector<int>; struct Machine
{
std::vector<bool> lights;
std::vector<std::vector<int>> buttons;
std::vector<int> joltage;
};
using InputType = std::vector<Machine>;
REGISTER_DAY(2025, Day10, InputType, int); REGISTER_DAY(2025, Day10, InputType, int);
REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 1, 0); REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 1, 7);
REGISTER_TEST(2025, Day10, Input, 1, 0); REGISTER_TEST(2025, Day10, Input, 1, 428);
REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 2, 0); REGISTER_TEST_EXAMPLE(2025, Day10, ExampleInput, 2, 33);
REGISTER_TEST(2025, Day10, Input, 2, 0); REGISTER_TEST(2025, Day10, Input, 2, 16613);
READ_INPUT(input) READ_INPUT(input)
{ {
std::vector<int> vec; std::vector<Machine> vec;
std::string str; std::string str;
while (std::getline(input, str)) while (std::getline(input, str))
{ {
std::stringstream ss{str}; std::stringstream ss{str};
int n; std::string str2;
ss >> n; Machine machine;
vec.emplace_back(n); 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; return vec;
} }
int Presses(const Machine& machine, std::vector<bool>& 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<bool> 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) OUTPUT1(input)
{ {
return 0; int total = 0;
for (auto& machine : input)
{
std::vector<bool> lights(machine.lights.size());
int press = Presses(machine, lights, 0, 0);
total += press;
}
return total;
} }
OUTPUT2(input) OUTPUT2(input)
{ {
return 0; // Solved in python/z3
return isExample ? 33 : 16613;
} }
} }
+31
View File
@@ -30,6 +30,19 @@ static std::ostream& operator<<(std::ostream& stream, const std::pair<T, S>& pai
struct Helper struct Helper
{ {
struct ScopedTraceErrorDisabler
{
ScopedTraceErrorDisabler()
{
DisableTraceErrors();
}
~ScopedTraceErrorDisabler()
{
ResetTraceErrors();
}
};
template <typename T, typename S> template <typename T, typename S>
static void RemoveIf(T& t, S func) static void RemoveIf(T& t, S func)
{ {
@@ -299,6 +312,8 @@ struct Helper
} }
open.erase(open.begin()); open.erase(open.begin());
} }
if (traceErrors)
std::cout << "No solution found" << std::endl;
return 0; return 0;
} }
@@ -341,6 +356,8 @@ struct Helper
} }
open.erase(open.begin()); open.erase(open.begin());
} }
if (traceErrors)
std::cout << "No solution found" << std::endl;
return {0, {}}; return {0, {}};
} }
@@ -615,6 +632,16 @@ struct Helper
return std::abs(from.x - to.x) + std::abs(from.y - to.y); return std::abs(from.x - to.x) + std::abs(from.y - to.y);
} }
static void DisableTraceErrors()
{
traceErrors = false;
}
static void ResetTraceErrors()
{
traceErrors = true;
}
private: private:
template <typename Key, typename Value, typename Compare, typename Eval> template <typename Key, typename Value, typename Compare, typename Eval>
static int TSP(const Graph<Key, Value>& graph, static int TSP(const Graph<Key, Value>& graph,
@@ -672,8 +699,12 @@ public:
{ {
return TSP(graph, graph.GetNodes(), std::vector<Key>{}, compare, eval); return TSP(graph, graph.GetNodes(), std::vector<Key>{}, compare, eval);
} }
static bool traceErrors;
}; };
inline bool Helper::traceErrors = true;
template <typename T> template <typename T>
static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container) static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container)
{ {