day two part one
This commit is contained in:
@@ -16,6 +16,10 @@
|
|||||||
<Content Include="Input\Test.txt">
|
<Content Include="Input\Test.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<None Remove="Input\DayTwo.txt" />
|
||||||
|
<Content Include="Input\DayTwo.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
41
AdventOfCode2025/GiftShop.cs
Normal file
41
AdventOfCode2025/GiftShop.cs
Normal 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);
|
||||||
@@ -4,3 +4,6 @@ Console.WriteLine("Advent of Code 2025");
|
|||||||
|
|
||||||
var dayOneInput = await File.ReadAllTextAsync("./Input/DayOne.txt");
|
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