Add solution for "Day 13: Claw Contraption", part 2

This commit is contained in:
2025-02-18 16:11:29 +01:00
parent e6a132521b
commit 973f62163d
4 changed files with 25 additions and 9 deletions

View File

@@ -60,7 +60,8 @@ void ClawContraption::processDataLine(const std::string& line)
stream >> x;
std::getline(stream, token, '=');
stream >> y;
part1 += calcTokenCost(buttonA_, buttonB_, { x, y });
part1 += calcTokenCost(buttonA_, buttonB_, { x, y }, 0);
part2 += calcTokenCost(buttonA_, buttonB_, { x, y }, getConversionCorrection());
}
}
}
@@ -84,17 +85,24 @@ constexpr int ClawContraption::getButtonBCost()
return 1;
}
long long int ClawContraption::calcTokenCost(const Point2& buttonA, const Point2& buttonB, const Point2& prize)
constexpr long long int ClawContraption::getConversionCorrection()
{
long long int p{ prize.y * buttonB.x - prize.x * buttonB.y };
return 10000000000000;
}
long long int ClawContraption::calcTokenCost(const Point2& buttonA, const Point2& buttonB, const Point2& prize,
const long long int offset)
{
long long int p{ (prize.y + offset) * buttonB.x - (prize.x + offset) * buttonB.y };
long long int q{ buttonA.y * buttonB.x - buttonA.x * buttonB.y };
long long int a{ p / q };
if (a * q != p || a > getNMaxButtonPushes())
if (a * q != p || (offset == 0 && a > getNMaxButtonPushes()))
{
return 0;
}
long long int b{ (prize.x - a * buttonA.x) / buttonB.x };
if (b > getNMaxButtonPushes())
long long int r{ prize.x + offset - a * buttonA.x };
long long int b{ r / buttonB.x };
if (b * buttonB.x != r || (offset == 0 && b > getNMaxButtonPushes()))
{
return 0;
}