Skip to content

Instantly share code, notes, and snippets.

@thatsumoguy
Created December 3, 2024 05:53
using System.Text.RegularExpressions;
using AoC2024.Extensions;
namespace AoC2024.Days
{
internal partial class Day3
{
public static string input
{
get;
set;
}
private static List<(int,int)> Muls = [];
private static List<(int,int)> Dos = [];
private static void Parse()
{
var reg = MyRegex().Matches(input);
var doIt = true;
foreach(Match r in reg)
{
if (r.Groups[1].Success)
{
doIt = true;
}
else if (r.Groups[2].Success) doIt = false;
if(r.Groups[3].Success)
{
var (num1, num2) = r.Value.ExtractNumbers<int>() switch { var a => (a[0], a[1]) };
Muls.Add((num1, num2));
if(doIt) Dos.Add((num1, num2));
}
}
}
public static int PartOne()
{
Parse();
return Muls.Sum(p => p.Item1 * p.Item2);
}
public static int PartTwo() => Dos.Sum(p => p.Item1 * p.Item2);
[GeneratedRegex(@"(do\(\))|(don't\(\))|(mul\(\d+,\d+\))")]
private static partial Regex MyRegex();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment