day two part one

This commit is contained in:
2025-12-02 10:31:01 -05:00
parent 6e4c1be9b7
commit 8dbf5946ee
3 changed files with 49 additions and 1 deletions

View File

@@ -16,6 +16,10 @@
<Content Include="Input\Test.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Remove="Input\DayTwo.txt" />
<Content Include="Input\DayTwo.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,41 @@
using System.Text.RegularExpressions;
namespace AdventOfCode2025;
public class GiftShop : IAdventSolution
{
public AdventSolution Solve(string input)
{
var ranges = input.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
ulong totalInvalid = 0;
foreach (var rangeString in ranges)
{
var range = GetRange(rangeString);
for (var i = range.Min; i <= range.Max; i++)
{
if(IsInvalid(i))
totalInvalid += (ulong)i;
}
}
return new AdventSolution(totalInvalid.ToString(), null);
}
private bool IsInvalid(ulong id)
{
var s = id.ToString();
var half = s.Length / 2;
var first = s[..half];
var second = s[half..];
return first == second;
}
private Range GetRange(string input)
{
var numbers = input.Split('-', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
return new Range(ulong.Parse(numbers[0]), ulong.Parse(numbers[^1]));
}
}
public record struct Range(ulong Min, ulong Max);

View File

@@ -4,3 +4,6 @@ Console.WriteLine("Advent of Code 2025");
var dayOneInput = await File.ReadAllTextAsync("./Input/DayOne.txt");
AdventSolver<SecretEntrance>.Solve(dayOneInput, 1, "Secret Entrance");
var dayTwoInput = await File.ReadAllTextAsync("./Input/DayTwo.txt");
AdventSolver<GiftShop>.Solve(dayTwoInput, 2, "Gift Shop");