Add solution for "Day 11: Plutonian Pebbles", part 2

This commit is contained in:
2025-01-22 20:38:08 +01:00
parent eaee3bd347
commit 21279a97a6
4 changed files with 37 additions and 14 deletions

View File

@@ -31,9 +31,9 @@ const std::string PlutonianPebbles::getInputFileName() const
void PlutonianPebbles::processDataLine(const std::string& line)
{
// Maps pebble numbers before the blink to their cardinality.
auto currentPebbles = std::make_shared<std::map<long long int, int>>();
auto currentPebbles = std::make_shared<std::map<long long int, long long int>>();
// Maps pebble numbers after the blink to their cardinality.
std::shared_ptr<std::map<long long int, int>> nextBlinkPebbles;
std::shared_ptr<std::map<long long int, long long int>> nextBlinkPebbles;
// Adds initial pebbles numbers.
std::istringstream lineStream{ line };
@@ -44,10 +44,10 @@ void PlutonianPebbles::processDataLine(const std::string& line)
}
// Processes the blinks.
for (int i = getNBlinks(); i > 1; i--)
for (int i = getNBlinksPart2(); i > 1; i--)
{
// Maps pebble numbers after the blink to their cardinality.
nextBlinkPebbles = std::make_shared<std::map<long long int, int>>();
nextBlinkPebbles = std::make_shared<std::map<long long int, long long int>>();
for (const auto& pebble : *currentPebbles)
{
@@ -64,21 +64,31 @@ void PlutonianPebbles::processDataLine(const std::string& line)
}
}
if (i == getNBlinksPart2() - getNBlinksPart1() + 1)
{
for (const auto& blinkNumber : *currentPebbles)
{
// The number is guaranteed to be in blinkMap_ already.
auto hit = blinkMap_.find(blinkNumber.first);
part1 += blinkNumber.second * hit->second.size();
}
}
currentPebbles = nextBlinkPebbles;
}
for (const auto nextBlinkNumber : *nextBlinkPebbles)
for (const auto& nextBlinkNumber : *nextBlinkPebbles)
{
// In the last step we only care about cardinalities, not the actual new pebble numbers, so we use
// getNNextBlinkNumbers() instead of addNextBlinkNumbers().
auto hit = blinkMap_.find(nextBlinkNumber.first);
if (hit == blinkMap_.end())
{
part1 += nextBlinkNumber.second * getNNextBlinkNumbers(nextBlinkNumber.first);
part2 += nextBlinkNumber.second * getNNextBlinkNumbers(nextBlinkNumber.first);
}
else
{
part1 += nextBlinkNumber.second * hit->second.size();
part2 += nextBlinkNumber.second * hit->second.size();
}
}
}
@@ -87,13 +97,18 @@ void PlutonianPebbles::finish()
{
}
constexpr int PlutonianPebbles::getNBlinks()
constexpr int PlutonianPebbles::getNBlinksPart1()
{
return 25;
}
void PlutonianPebbles::addPebble(std::map<long long int, int>& pebbles, const long long int pebbleNumber,
int cardinality)
constexpr int PlutonianPebbles::getNBlinksPart2()
{
return 75;
}
void PlutonianPebbles::addPebble(std::map<long long int, long long int>& pebbles, const long long int pebbleNumber,
long long int cardinality)
{
auto hit = pebbles.find(pebbleNumber);
if (hit == pebbles.end())