using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace Advent24 { internal class Day3 { public Day3() { string fileData = System.IO.File.ReadAllText(@"..\..\..\inputd3.txt"); string pattern = @"((mul\((?\d+),(?\d+)\))|(do\(\))|(don't\(\)))"; Regex r = new(pattern, RegexOptions.None, TimeSpan.FromMilliseconds(150)); Match m = r.Match(fileData); Int32 productSum = 0; Boolean mulAlwaysEnabled = false; Boolean mulEnabled = true; while (m.Success) { // do() matched if (m.Groups[3].Success) { mulEnabled = true; Console.WriteLine("mulEnabled = {0:s}", mulEnabled); } // don't() matched else if (m.Groups[4].Success) { mulEnabled = false; Console.WriteLine("mulEnabled = {0:s}", mulEnabled); } // neither do() nor don't() matched else { if (mulEnabled || mulAlwaysEnabled) { productSum += System.Convert.ToInt32(m.Groups["Multiplicand"].Value) * System.Convert.ToInt32(m.Groups["Multiplier"].Value); Console.WriteLine("{0:s} * {1:s}", m.Groups["Multiplicand"].Value, m.Groups["Multiplier"].Value); } } // look at the next match m = m.NextMatch(); } Console.WriteLine("productSum = {0:d}", productSum); // to keep the console window from closing Console.ReadKey(); } } }