Skip to content

Commit 13caea1

Browse files
committed
2024 day 3 part 2
1 parent e8d192a commit 13caea1

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

2024/Day03/Solution.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Runtime;
56
using System.Security.Cryptography.X509Certificates;
67
using System.Text.RegularExpressions;
@@ -45,6 +46,60 @@ public void SolvePart1()
4546

4647
public void SolvePart2()
4748
{
48-
throw new NotImplementedException();
49+
//var inputFilePath = GetInputFilePath("testtwo");
50+
var inputFilePath = GetInputFilePath();
51+
52+
string[] lines = File.ReadAllLines(inputFilePath);
53+
54+
string regexpattern = @"(?:\w*mul\(\d{1,3},\d{1,3}\))|(?:\b(?:un)?do\(\))|(?:\bdon't\(\))";
55+
56+
List<string> allmatches = new();
57+
58+
long totalSum = 0;
59+
bool doit = true;
60+
foreach (var line in lines)
61+
{
62+
var matches = Regex.Matches(line, regexpattern);
63+
64+
foreach(Match match in matches)
65+
{
66+
allmatches.Add(match.Value);
67+
68+
if(match.ToString().Contains("mul"))
69+
{
70+
if (doit)
71+
{
72+
string temp = match.Value;
73+
74+
if (temp.Contains("mul"))
75+
{
76+
// Normalize anything ending in "mul" (e.g., xmul, _mul) to "mul"
77+
temp = Regex.Replace(temp, @"\w*mul", "mul");
78+
}
79+
80+
string item = temp.Replace("mul(", "").Replace(")", "");
81+
82+
long x = long.Parse(item.Split(',')[0]);
83+
long y = long.Parse(item.Split(',')[1]);
84+
85+
totalSum += x * y;
86+
}
87+
}
88+
else
89+
{
90+
if (match.ToString().ToLower().Contains("don't"))
91+
{
92+
doit = false;
93+
}
94+
else
95+
{
96+
doit = true;
97+
}
98+
}
99+
100+
}
101+
}
102+
103+
Console.WriteLine($"total sum is: {totalSum}");
49104
}
50105
}

input

Submodule input updated from 2481d14 to 211af8b

0 commit comments

Comments
 (0)