From 55e7f3c86217e6030eb8215ed70cdab2fb93ee46 Mon Sep 17 00:00:00 2001 From: SebastianStork Date: Wed, 11 Dec 2024 21:04:46 +0100 Subject: [PATCH] Solve 2024 day 3 part 2 --- 2024/day-03/main.cpp | 58 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/2024/day-03/main.cpp b/2024/day-03/main.cpp index f73f029..5228b6c 100644 --- a/2024/day-03/main.cpp +++ b/2024/day-03/main.cpp @@ -23,15 +23,39 @@ bool isNumber(const string &potentialNumber) return true; } -string getNextValidInstruction(string &line) +string getNextValidInstruction(string &line, const bool considerConditionals, bool &doMultiplication) { while (true) { { - size_t startPosition = line.find("mul("); - if (startPosition == string::npos) { + size_t mulPosition = line.find("mul("); + if (mulPosition == string::npos) { return ""; } - line.erase(0, startPosition); + + if (considerConditionals) { + size_t doPosition = line.find("do()"); + size_t dontPosition = line.find("don't()"); + if ((doPosition < mulPosition) && (dontPosition < mulPosition)) { + if (doPosition > dontPosition) { + doMultiplication = true; + } else { + doMultiplication = false; + } + } else { + if (doPosition < mulPosition) { + doMultiplication = true; + } else if (dontPosition < mulPosition) { + doMultiplication = false; + } + } + } + + line.erase(0, mulPosition); + } + + if (considerConditionals && !doMultiplication) { + line.erase(0, 4); + continue; } size_t commaPosition = line.find(','); @@ -74,20 +98,34 @@ int getInstructionResult(const string &instruction) return number1 * number2; } -int main() +int getResult(vector lines, const bool considerConditionals) { - vector lines = readInput(); - int finalResult = 0; + int result = 0; + bool doMultiplication = true; for (string &line : lines) { while (true) { - string instruction = getNextValidInstruction(line); + string instruction = getNextValidInstruction(line, + considerConditionals, + doMultiplication); if (instruction == "") { break; } - finalResult += getInstructionResult(instruction); + result += getInstructionResult(instruction); } } - cout << "Sum of all the multiplication: " << finalResult << endl; + return result; +} + +int main() +{ + vector lines = readInput(); + + // Part one + cout << "Sum of all the multiplication: " << getResult(lines, false) << endl; + + // Part two + cout << "Sum of all the multiplication (when taking the conditional statements into account): " + << getResult(lines, true) << endl; }