-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.cs
37 lines (30 loc) · 1.15 KB
/
Day3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Text.RegularExpressions;
namespace Aoc2024Net.Days
{
internal sealed class Day3 : Day
{
public override object? SolvePart1() => Solve(false);
public override object? SolvePart2() => Solve(true);
private long Solve(bool useSwitchInstructions)
{
const string disableInstruction = "don't()";
const string enableInstruction = "do()";
var input = InputData.GetInputText();
var matches = Regex.Matches(
input,
@$"mul\((\d{{1,3}}),(\d{{1,3}})\){(useSwitchInstructions ? @$"|{Regex.Escape(disableInstruction)}|{Regex.Escape(enableInstruction)}" : string.Empty)}");
var enabled = true;
var result = 0;
foreach (Match m in matches)
{
if (m.Value == disableInstruction)
enabled = false;
else if (m.Value == enableInstruction)
enabled = true;
else if (enabled)
result += int.Parse(m.Groups[1].Value) * int.Parse(m.Groups[2].Value);
}
return result;
}
}
}