-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03.cs
48 lines (37 loc) · 1 KB
/
Day03.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
38
39
40
41
42
43
44
45
46
47
48
using System.Text.RegularExpressions;
namespace advent_of_code_csharp_2024;
public class Day03
{
private static int ProcessMul(Match m)
{
var x = int.Parse(m.Groups[1].Value);
var y = int.Parse(m.Groups[2].Value);
return x * y;
}
public static int RunProgram(string testInput)
{
var regex = new Regex("""mul\(([0-9]+),([0-9]+)\)""");
var matches = regex.Matches(testInput);
var ans = matches
.Select(ProcessMul)
.Sum();
return ans;
}
public static int RunProgramPartII(string testInput)
{
var regex = new Regex("""mul\(([0-9]+),([0-9]+)\)|don't\(\)|do\(\)""");
var matches = regex.Matches(testInput);
bool enabled = true;
int total = 0;
foreach (Match match in matches)
{
if (match.Value == "do()")
enabled = true;
else if (match.Value == "don't()")
enabled = false;
else if (enabled)
total += ProcessMul(match);
}
return total;
}
}