Skip to content

Commit

Permalink
day 3 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 3, 2024
1 parent c7a1afe commit c9cc77e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion AdventOfCode/Day03.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public override ValueTask<string> Solve_1()

public override ValueTask<string> Solve_2()
{
return new("");
long result = 0;

MatchCollection matches = Regex.Matches(_input, @"mul\([0-9]+,[0-9]+\)|do\(\)|don't\(\)");

bool enabled = true;
foreach (Match match in matches)
{
string value = match.Captures[0].Value;

if (value == "do()")
{
enabled = true;
}
else if (value == "don't()")
{
enabled = false;
}
else if (enabled)
{
string[] parts = value.Split(new[] { "mul(", ",", ")" }, StringSplitOptions.RemoveEmptyEntries);
result += long.Parse(parts[0]) * long.Parse(parts[1]);
}
}

return new(result.ToString());
}
}

0 comments on commit c9cc77e

Please sign in to comment.