diff --git a/res/2025/day08/test_input.txt b/res/2025/day08/test_input.txt new file mode 100644 index 0000000..e98a3b6 --- /dev/null +++ b/res/2025/day08/test_input.txt @@ -0,0 +1,20 @@ +162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689 diff --git a/src/2025/Day08.cpp b/src/2025/Day08.cpp index d49fa2d..f3c6854 100644 --- a/src/2025/Day08.cpp +++ b/src/2025/Day08.cpp @@ -2,35 +2,114 @@ namespace y2025::day08 { - using InputType = std::vector; - REGISTER_DAY(2025, Day08, InputType, int); + using InputType = std::vector; + REGISTER_DAY(2025, Day08, InputType, int64_t); - REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 1, 0); - REGISTER_TEST(2025, Day08, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 2, 0); - REGISTER_TEST(2025, Day08, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 1, 40); + REGISTER_TEST(2025, Day08, Input, 1, 122636); + REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 2, 25272); + REGISTER_TEST(2025, Day08, Input, 2, 9271575747); 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); + Index3D index; + ss >> index.x >> "," >> index.y >> "," >> index.z; + vec.emplace_back(index); } return vec; } + std::multimap> CalculateDistances(const std::vector& indices) + { + std::multimap> distances; + for (int i = 0; i < indices.size(); i++) + { + for (int j = i + 1; j < indices.size(); j++) + { + Index3D offset = indices[i] - indices[j]; + int64_t distance = (int64_t)offset.x * (int64_t)offset.x + (int64_t)offset.y * (int64_t)offset.y + + (int64_t)offset.z * (int64_t)offset.z; + distances.emplace(distance, std::pair{indices[i], indices[j]}); + } + } + return distances; + } + + void Connect(Index3D i1, Index3D i2, std::vector>& circuits) + { + int n1 = -1; + int n2 = -1; + for (int j = 0; j < circuits.size(); j++) + { + if (circuits[j].count(i1) != 0) + n1 = j; + if (circuits[j].count(i2) != 0) + n2 = j; + if (n1 != -1 && n2 != -1) + break; + } + + if (n1 == -1 && n2 == -1) + { + circuits.emplace_back(std::set{i1, i2}); + } + else if (n1 != -1 && n2 == -1) + { + circuits[n1].emplace(i2); + } + else if (n1 == -1 && n2 != -1) + { + circuits[n2].emplace(i1); + } + else if (n1 != -1 && n2 != -1 && n1 != n2) + { + std::set cpy = circuits[n2]; + circuits[n1].insert(cpy.begin(), cpy.end()); + circuits.erase(circuits.begin() + n2); + } + } + OUTPUT1(input) { - return 0; + std::multimap> distances = CalculateDistances(input); + std::vector> circuits; + auto it = distances.begin(); + int count = isExample ? 10 : 1000; + for (int i = 0; i < count; i++, it++) + { + Connect(it->second.first, it->second.second, circuits); + } + std::sort(circuits.begin(), + circuits.end(), + [](const std::set& set1, const std::set& set2) { return set1.size() > set2.size(); }); + int64_t product = 1; + for (int i = 0; i < 3; i++) + { + product *= circuits[i].size(); + } + return product; } OUTPUT2(input) { - return 0; + std::multimap> distances = CalculateDistances(input); + std::vector> circuits; + for (int i = 0; i < input.size(); i++) + { + circuits.emplace_back(std::set{input[i]}); + } + auto it = distances.begin(); + for (; circuits.size() != 1; it++) + { + Connect(it->second.first, it->second.second, circuits); + } + it--; + return (int64_t)it->second.first.x * (int64_t)it->second.second.x; } }