From 7a06bc717206063d60610a13b6c344d3215a4829 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Tue, 3 Dec 2024 12:04:17 +0100 Subject: [PATCH] AdventOfCode2024 Day03 Part 2 --- AdventOfCode2024.Tests/Day03_Tests.cs | 12 +++++++ AdventOfCode2024/Day03.cs | 50 +++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/AdventOfCode2024.Tests/Day03_Tests.cs b/AdventOfCode2024.Tests/Day03_Tests.cs index 161bdcf..8959f7a 100644 --- a/AdventOfCode2024.Tests/Day03_Tests.cs +++ b/AdventOfCode2024.Tests/Day03_Tests.cs @@ -13,4 +13,16 @@ public class Day03_Tests 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); + } } \ No newline at end of file diff --git a/AdventOfCode2024/Day03.cs b/AdventOfCode2024/Day03.cs index 4264756..c8b2d17 100644 --- a/AdventOfCode2024/Day03.cs +++ b/AdventOfCode2024/Day03.cs @@ -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? +--- 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? */ @@ -44,12 +64,38 @@ public class Day03 : IDay total += value0 * value1; } } - + return total.ToString(); } 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(); } } \ No newline at end of file