This commit is contained in:
2024-12-03 18:47:45 -05:00
parent 001f4bcc79
commit 0a6f1a5c8c

View File

@@ -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<int, int>
{
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\\((?<a>\\d+)\\,(?<b>\\d+)\\))|(do\\(\\))|(don't\\(\\))")]
private static partial Regex DosAndDonts();
[GeneratedRegex("mul\\((?<a>\\d+)\\,(?<b>\\d+)\\)")]
private static partial Regex Mul();
}