Skip to content

Commit dd2c771

Browse files
committed
2024 Day 3
1 parent 6bba11e commit dd2c771

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/2024/03/Program.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
using AdventOfCode.Common;
1+
using System.Text.RegularExpressions;
2+
using AdventOfCode.Common;
23

3-
var lines = Resources.GetInputFileLines("input.txt");
4+
var content = Resources.GetInputFileContent("input.txt");
5+
var mulRegex = new Regex(@"(?<instruction>mul\((?<number1>\d+),(?<number2>\d+)\)|do\(\)|don't\(\))");
46

5-
Console.WriteLine($"Part 1: {""}");
6-
Console.WriteLine($"Part 2: {""}");
7+
var disabled = false;
8+
uint sum = 0;
9+
uint enabledOnlySum = 0;
10+
foreach (Match match in mulRegex.Matches(content))
11+
{
12+
var instruction = match.Groups["instruction"].Value;
13+
14+
switch (instruction)
15+
{
16+
case "do()":
17+
disabled = false;
18+
continue;
19+
case "don't()":
20+
disabled = true;
21+
continue;
22+
default:
23+
var number1 = uint.Parse(match.Groups["number1"].Value);
24+
var number2 = uint.Parse(match.Groups["number2"].Value);
25+
var mul = number1 * number2;
26+
sum += mul;
27+
if (!disabled)
28+
{
29+
enabledOnlySum += mul;
30+
}
31+
32+
continue;
33+
}
34+
}
35+
36+
Console.WriteLine($"Part 1: {sum}");
37+
Console.WriteLine($"Part 2: {enabledOnlySum}");

0 commit comments

Comments
 (0)