mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 14:31:34 +01:00
Solve 2024 day 7 part 2
This commit is contained in:
parent
c644cf55d8
commit
a9afc371bb
1 changed files with 40 additions and 16 deletions
|
|
@ -31,28 +31,43 @@ void readInput(vector<Equation> &equations)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long computeEquationResult(const vector<int> &numbers, const size_t operatorMask)
|
long computeEquationResult(const vector<int> &numbers,
|
||||||
|
size_t operatorMask,
|
||||||
|
const size_t operatorSpace)
|
||||||
{
|
{
|
||||||
long testResult = numbers[0];
|
long currentResult = numbers[0];
|
||||||
|
|
||||||
for (size_t numberIndex = 1; numberIndex < numbers.size(); numberIndex++) {
|
for (size_t operatorIndex = 0; operatorIndex < numbers.size() - 1; operatorIndex++) {
|
||||||
if (operatorMask & (1 << (numberIndex - 1))) {
|
size_t numberIndex = operatorIndex + 1;
|
||||||
testResult += numbers[numberIndex];
|
|
||||||
} else {
|
int currentOperator = (operatorMask / static_cast<int>(pow(operatorSpace, operatorIndex)))
|
||||||
testResult *= numbers[numberIndex];
|
% operatorSpace;
|
||||||
|
|
||||||
|
switch (currentOperator) {
|
||||||
|
case 0:
|
||||||
|
currentResult += numbers[numberIndex];
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
currentResult *= numbers[numberIndex];
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
currentResult = stol(to_string(currentResult) + to_string(numbers[numberIndex]));
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return testResult;
|
return currentResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValidEquation(const Equation &equation)
|
bool isValidEquation(const Equation &equation, const size_t operatorSpace)
|
||||||
{
|
{
|
||||||
const int operatorCount = equation.numbers.size() - 1;
|
const size_t operatorCount = equation.numbers.size() - 1;
|
||||||
const size_t operatorMaskLimit = pow(2, operatorCount) - 1;
|
const size_t maxOperatorMask = pow(operatorSpace, operatorCount) - 1;
|
||||||
|
|
||||||
for (size_t operatorMask = 0; operatorMask <= operatorMaskLimit; operatorMask++) {
|
for (size_t operatorMask = 0; operatorMask <= maxOperatorMask; operatorMask++) {
|
||||||
if (computeEquationResult(equation.numbers, operatorMask) == equation.result) {
|
if (computeEquationResult(equation.numbers, operatorMask, operatorSpace)
|
||||||
|
== equation.result) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -65,11 +80,20 @@ int main()
|
||||||
vector<Equation> equations;
|
vector<Equation> equations;
|
||||||
readInput(equations);
|
readInput(equations);
|
||||||
|
|
||||||
long totalCalibrationResult = 0;
|
long totalResult = 0;
|
||||||
|
long totalResultWithConcatenation = 0;
|
||||||
for (const Equation &equation : equations) {
|
for (const Equation &equation : equations) {
|
||||||
totalCalibrationResult += equation.result * isValidEquation(equation);
|
if (isValidEquation(equation, 2)) {
|
||||||
|
totalResult += equation.result;
|
||||||
|
totalResultWithConcatenation += equation.result;
|
||||||
|
} else if (isValidEquation(equation, 3)) {
|
||||||
|
totalResultWithConcatenation += equation.result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Part one
|
// Part one
|
||||||
cout << "Total Calibration Result: " << totalCalibrationResult << endl;
|
cout << "Total calibration result: " << totalResult << endl;
|
||||||
|
|
||||||
|
// Part one
|
||||||
|
cout << "Total calibration result with concatenation: " << totalResultWithConcatenation << endl;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue