Compare commits
2 Commits
6e4c1be9b7
...
1ac8cee3d5
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ac8cee3d5 | |||
| 8dbf5946ee |
@@ -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>
|
||||
|
||||
48
AdventOfCode2025/GiftShop.cs
Normal file
48
AdventOfCode2025/GiftShop.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AdventOfCode2025;
|
||||
|
||||
public class GiftShop : IAdventSolution
|
||||
{
|
||||
private readonly Regex _regex = new(@"^(.+)\1+$");
|
||||
|
||||
public AdventSolution Solve(string input)
|
||||
{
|
||||
var ranges = input.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
ulong totalInvalid = 0;
|
||||
ulong newInvalid = 0;
|
||||
foreach (var rangeString in ranges)
|
||||
{
|
||||
var range = GetRange(rangeString);
|
||||
for (var i = range.Min; i <= range.Max; i++)
|
||||
{
|
||||
if (IsInvalid(i))
|
||||
totalInvalid += i;
|
||||
|
||||
if(IsRepeat(i))
|
||||
newInvalid += i;
|
||||
}
|
||||
}
|
||||
|
||||
return new AdventSolution(totalInvalid.ToString(),newInvalid.ToString());
|
||||
}
|
||||
|
||||
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 bool IsRepeat(ulong id) => _regex.IsMatch(id.ToString());
|
||||
|
||||
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);
|
||||
@@ -3,4 +3,7 @@
|
||||
Console.WriteLine("Advent of Code 2025");
|
||||
|
||||
var dayOneInput = await File.ReadAllTextAsync("./Input/DayOne.txt");
|
||||
AdventSolver<SecretEntrance>.Solve(dayOneInput, 1, "Secret Entrance");
|
||||
AdventSolver<SecretEntrance>.Solve(dayOneInput, 1, "Secret Entrance");
|
||||
|
||||
var dayTwoInput = await File.ReadAllTextAsync("./Input/DayTwo.txt");
|
||||
AdventSolver<GiftShop>.Solve(dayTwoInput, 2, "Gift Shop");
|
||||
Reference in New Issue
Block a user