Add solution for 2025 Day 8
This commit is contained in:
@@ -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
|
||||||
+90
-11
@@ -2,35 +2,114 @@
|
|||||||
|
|
||||||
namespace y2025::day08
|
namespace y2025::day08
|
||||||
{
|
{
|
||||||
using InputType = std::vector<int>;
|
using InputType = std::vector<Index3D>;
|
||||||
REGISTER_DAY(2025, Day08, InputType, int);
|
REGISTER_DAY(2025, Day08, InputType, int64_t);
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 1, 0);
|
REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 1, 40);
|
||||||
REGISTER_TEST(2025, Day08, Input, 1, 0);
|
REGISTER_TEST(2025, Day08, Input, 1, 122636);
|
||||||
REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 2, 0);
|
REGISTER_TEST_EXAMPLE(2025, Day08, ExampleInput, 2, 25272);
|
||||||
REGISTER_TEST(2025, Day08, Input, 2, 0);
|
REGISTER_TEST(2025, Day08, Input, 2, 9271575747);
|
||||||
|
|
||||||
READ_INPUT(input)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
std::vector<Index3D> 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;
|
int n;
|
||||||
ss >> n;
|
Index3D index;
|
||||||
vec.emplace_back(n);
|
ss >> index.x >> "," >> index.y >> "," >> index.z;
|
||||||
|
vec.emplace_back(index);
|
||||||
}
|
}
|
||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::multimap<int64_t, std::pair<Index3D, Index3D>> CalculateDistances(const std::vector<Index3D>& indices)
|
||||||
|
{
|
||||||
|
std::multimap<int64_t, std::pair<Index3D, Index3D>> 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<std::set<Index3D>>& 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<Index3D> cpy = circuits[n2];
|
||||||
|
circuits[n1].insert(cpy.begin(), cpy.end());
|
||||||
|
circuits.erase(circuits.begin() + n2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
OUTPUT1(input)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
std::multimap<int64_t, std::pair<Index3D, Index3D>> distances = CalculateDistances(input);
|
||||||
|
std::vector<std::set<Index3D>> 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<Index3D>& set1, const std::set<Index3D>& 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)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
std::multimap<int64_t, std::pair<Index3D, Index3D>> distances = CalculateDistances(input);
|
||||||
|
std::vector<std::set<Index3D>> 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user