AdventOfCode2024 Day03 Part 2

This commit is contained in:
2024-12-03 12:04:17 +01:00
parent 2662c17c60
commit 7a06bc7172
2 changed files with 60 additions and 2 deletions

View File

@@ -13,4 +13,16 @@ public class Day03_Tests
Assert.Equal("161", result); Assert.Equal("161", result);
} }
[Fact]
public void ResolvePart2__Example()
{
Day03 day = new();
string result = day.ResolvePart2([
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))",
]);
Assert.Equal("48", result);
}
} }

View File

@@ -23,6 +23,26 @@ Only the four highlighted sections are real mul instructions. Adding up the resu
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications? Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
--- Part Two ---
As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result.
There are two new instructions you'll need to handle:
The do() instruction enables future mul instructions.
The don't() instruction disables future mul instructions.
Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.
For example:
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
This corrupted memory is similar to the example from before, but this time the mul(5,5) and mul(11,8) instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.
This time, the sum of the results is 48 (2*4 + 8*5).
Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
*/ */
@@ -50,6 +70,32 @@ public class Day03 : IDay
public string ResolvePart2(string[] inputs) public string ResolvePart2(string[] inputs)
{ {
return string.Empty; Regex regex = new(@"mul\((\d*\d)\,(\d*\d)\)|do\(\)|don't\(\)");
int total = 0;
bool enabled = true;
foreach (string input in inputs)
{
MatchCollection matches = regex.Matches(input);
foreach (Match match in matches)
{
switch (match.Groups[0].Value)
{
case "do()":
enabled = true;
continue;
case "don't()":
enabled = false;
continue;
}
if (enabled == false) { continue; }
int value0 = int.Parse(match.Groups[1].Value);
int value1 = int.Parse(match.Groups[2].Value);
total += value0 * value1;
}
}
return total.ToString();
} }
} }