diff --git a/AdventOfCode2025/AdventOfCode2025.csproj b/AdventOfCode2025/AdventOfCode2025.csproj index 272c720..88cf5a7 100644 --- a/AdventOfCode2025/AdventOfCode2025.csproj +++ b/AdventOfCode2025/AdventOfCode2025.csproj @@ -16,6 +16,10 @@ PreserveNewest + + + PreserveNewest + diff --git a/AdventOfCode2025/GiftShop.cs b/AdventOfCode2025/GiftShop.cs new file mode 100644 index 0000000..ba1cd4d --- /dev/null +++ b/AdventOfCode2025/GiftShop.cs @@ -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); \ No newline at end of file diff --git a/AdventOfCode2025/Program.cs b/AdventOfCode2025/Program.cs index db00078..3c3b3c3 100644 --- a/AdventOfCode2025/Program.cs +++ b/AdventOfCode2025/Program.cs @@ -3,4 +3,7 @@ Console.WriteLine("Advent of Code 2025"); var dayOneInput = await File.ReadAllTextAsync("./Input/DayOne.txt"); -AdventSolver.Solve(dayOneInput, 1, "Secret Entrance"); \ No newline at end of file +AdventSolver.Solve(dayOneInput, 1, "Secret Entrance"); + +var dayTwoInput = await File.ReadAllTextAsync("./Input/DayTwo.txt"); +AdventSolver.Solve(dayTwoInput, 2, "Gift Shop"); \ No newline at end of file