Skip to content

Commit

Permalink
day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mazharenko committed Dec 3, 2024
1 parent e787f02 commit 508a2de
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
56 changes: 42 additions & 14 deletions src/aoc/Year2024/Day03/Day03.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
using System.Text.RegularExpressions;
using mazharenko.AoCAgent.Generator;

namespace aoc.Year2024.Day03;

internal partial class Day03
{
internal partial class Part1
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
internal partial class Part1
{
private readonly Example example = new(
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))",
161
);

public long Solve(string input)
{
var regex = new Regex(@"mul\((?<operand1>\d+),(?<operand2>\d+)\)");
return regex.Matches(input).Sum(m =>
int.Parse(m.Groups["operand1"].Value)
* int.Parse(m.Groups["operand2"].Value)
);
}
}

internal partial class Part2
{
private readonly Example example = new(
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))",
48
);

public long Solve(string input)
{
var regex = new Regex(@"do\(\)|don't\(\)|mul\((?<operand1>\d+),(?<operand2>\d+)\)");

internal partial class Part2
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
return regex.Matches(input)
.Aggregate((enabled: true, sum: 0),
(x, match) => match.Value switch
{
"do()" => (true, x.sum),
"don't()" => (false, x.sum),
_ => x.enabled
? (true, x.sum + int.Parse(match.Groups["operand1"].Value) * int.Parse(match.Groups["operand2"].Value))
: x
}).sum;
}
}
}
16 changes: 9 additions & 7 deletions tests/AoC.Tests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace AoC.Tests;
[GenerateInputTests(2024, nameof(GetCases))]
internal partial class InputTests
{
private static IEnumerable<PartInputCaseData> GetCases()
{
yield return new PartInputCaseData(1, 1, "1889772");
yield return new PartInputCaseData(1, 2, "23228917");
yield return new PartInputCaseData(2, 1, "359");
yield return new PartInputCaseData(2, 2, "418");
}
private static IEnumerable<PartInputCaseData> GetCases()
{
yield return new PartInputCaseData(1, 1, "1889772");
yield return new PartInputCaseData(1, 2, "23228917");
yield return new PartInputCaseData(2, 1, "359");
yield return new PartInputCaseData(2, 2, "418");
yield return new PartInputCaseData(3, 1, "173529487");
yield return new PartInputCaseData(3, 2, "99532691");
}
}
Loading

0 comments on commit 508a2de

Please sign in to comment.