-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03.cs
More file actions
58 lines (52 loc) · 1.74 KB
/
Day03.cs
File metadata and controls
58 lines (52 loc) · 1.74 KB
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
49
50
51
52
53
54
55
56
57
58
using System.Text.RegularExpressions;
using _2024.Utils;
namespace _2024._03;
public class Day03 : Base
{
public Day03(bool example) : base(example)
{
Day = "3";
}
public override object PartOne()
{
string[] input = ReadInput();
int solution = 0;
Regex pattern = new Regex("mul\\([0-9]{1,3},[0-9]{1,3}\\)");
MatchCollection matches = pattern.Matches(String.Join("", input));
foreach (Match match in matches)
{
string[] numbers = match.Value.Replace("mul(", "").Replace(")", "").Split(",");
int a = int.Parse(numbers[0]);
int b = int.Parse(numbers[1]);
solution += (a * b);
}
return solution;
}
public override object PartTwo()
{
string[] input = ReadInput();
int solution = 0;
bool mulEnabled = true;
Regex pattern = new Regex("mul\\([0-9]{1,3},[0-9]{1,3}\\)|don't\\(\\)|do\\(\\)");
MatchCollection matches = pattern.Matches(String.Join("", input));
foreach (Match match in matches)
{
switch (match.Value)
{
case "do()":
mulEnabled = true;
break;
case "don't()":
mulEnabled = false;
break;
default:
string[] numbers = match.Value.Replace("mul(", "").Replace(")", "").Split(",");
int a = int.Parse(numbers[0]);
int b = int.Parse(numbers[1]);
solution += mulEnabled ? (a * b) : 0;
break;
}
}
return solution;
}
}