diff --git a/AdventOfCode/Problems/AOC2024/Day3/MullItOver.cs b/AdventOfCode/Problems/AOC2024/Day3/MullItOver.cs new file mode 100644 index 0000000..561ce28 --- /dev/null +++ b/AdventOfCode/Problems/AOC2024/Day3/MullItOver.cs @@ -0,0 +1,54 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace AdventOfCode.Problems.AOC2024.Day3; +[ProblemInfo(2024, 3, "Mull It Over")] +internal partial class MullItOver : Problem +{ + private string _data = string.Empty; + + public override void CalculatePart1() + { + var matches = Mul().Matches(_data); + Part1 = matches.Select(m => (int.Parse(m.Groups["a"].Value), int.Parse(m.Groups["b"].Value))).Select(v => v.Item1 * v.Item2).Sum(); + } + + public override void CalculatePart2() + { + var doing = true; + var muls = DosAndDonts().Matches(_data); + foreach (Match match in muls) + { + switch (match.Value) + { + case ['d', 'o', 'n', ..]: + doing = false; + break; + case ['d', 'o', ..]: + doing = true; + break; + default: + if (!doing) + continue; + Part2 += int.Parse(match.Groups["a"].Value) * int.Parse(match.Groups["b"].Value); + break; + } + } + } + + public override void LoadInput() + { + _data = ReadInputText("input.txt"); + } + + [GeneratedRegex("(mul\\((?\\d+)\\,(?\\d+)\\))|(do\\(\\))|(don't\\(\\))")] + private static partial Regex DosAndDonts(); + [GeneratedRegex("mul\\((?\\d+)\\,(?\\d+)\\)")] + private static partial Regex Mul(); +}