-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03.cs
43 lines (40 loc) · 1.27 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
using System.Text.RegularExpressions;
namespace AdventOfCode2024
{
internal class Day03(string input) : Solution(input)
{
readonly string input = input;
public override string Part1()
{
int total = 0;
Regex validMul = new Regex(@"mul\((\d{1,3}),(\d{1,3})\)");
foreach (Match match in validMul.Matches(input))
{
total += (int.Parse(match.Groups[1].Value) * int.Parse(match.Groups[2].Value));
}
return total.ToString();
}
public override string Part2()
{
int total = 0;
bool ignore = false;
Regex validMul = new Regex(@"mul\((\d{1,3}),(\d{1,3})\)|(do\(\)|don't\(\))");
foreach (Match match in validMul.Matches(input))
{
if (match.Value == "do()")
{
ignore = false;
}
else if (match.Value == "don't()")
{
ignore = true;
}
else if (!ignore)
{
total += (int.Parse(match.Groups[1].Value) * int.Parse(match.Groups[2].Value));
}
}
return total.ToString();
}
}
}