mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 21:31:33 +01:00
Solve 2024 day 3 part 2
This commit is contained in:
parent
1840ec7360
commit
55e7f3c862
1 changed files with 48 additions and 10 deletions
|
|
@ -23,15 +23,39 @@ bool isNumber(const string &potentialNumber)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
string getNextValidInstruction(string &line)
|
string getNextValidInstruction(string &line, const bool considerConditionals, bool &doMultiplication)
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
{
|
{
|
||||||
size_t startPosition = line.find("mul(");
|
size_t mulPosition = line.find("mul(");
|
||||||
if (startPosition == string::npos) {
|
if (mulPosition == string::npos) {
|
||||||
return "";
|
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(',');
|
size_t commaPosition = line.find(',');
|
||||||
|
|
@ -74,20 +98,34 @@ int getInstructionResult(const string &instruction)
|
||||||
return number1 * number2;
|
return number1 * number2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int getResult(vector<string> lines, const bool considerConditionals)
|
||||||
{
|
{
|
||||||
vector<string> lines = readInput();
|
int result = 0;
|
||||||
int finalResult = 0;
|
bool doMultiplication = true;
|
||||||
|
|
||||||
for (string &line : lines) {
|
for (string &line : lines) {
|
||||||
while (true) {
|
while (true) {
|
||||||
string instruction = getNextValidInstruction(line);
|
string instruction = getNextValidInstruction(line,
|
||||||
|
considerConditionals,
|
||||||
|
doMultiplication);
|
||||||
if (instruction == "") {
|
if (instruction == "") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
finalResult += getInstructionResult(instruction);
|
result += getInstructionResult(instruction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "Sum of all the multiplication: " << finalResult << endl;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
vector<string> 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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue