diff --git a/AdventOfCode2025/Day1.cs b/AdventOfCode2025/Day1.cs index 4a704db..aed981c 100644 --- a/AdventOfCode2025/Day1.cs +++ b/AdventOfCode2025/Day1.cs @@ -1,15 +1,17 @@ +using AdventOfCode2025.Inputs; + namespace AdventOfCode2025; public sealed class Day1 : Solution { + private readonly Input input = new(1); private const int MaxArrow = 100; public override string SolvePart1() { var arrow = 50; var zeroCounter = 0; - IEnumerable lines = InputReader.ReadLines(1); - foreach (var line in lines) + foreach (var line in input.Lines) { var number = int.Parse(line[1..]); arrow = line.First() switch @@ -33,9 +35,8 @@ public sealed class Day1 : Solution { var arrow = 50; var zeroCounter = 0; - IEnumerable lines = InputReader.ReadLines(1); - foreach (var line in lines) + foreach (var line in input.Lines) { var number = int.Parse(line[1..]); zeroCounter += number / MaxArrow; diff --git a/AdventOfCode2025/Day2.cs b/AdventOfCode2025/Day2.cs index 70e2974..91a2633 100644 --- a/AdventOfCode2025/Day2.cs +++ b/AdventOfCode2025/Day2.cs @@ -1,11 +1,13 @@ +using AdventOfCode2025.Inputs; + namespace AdventOfCode2025; public class Day2 : Solution { + private readonly Input input = new(2); public override string SolvePart1() { - return InputReader - .ReadLines(2) + return input.Lines .SelectMany(ToRanges) .Select(ToLimit) .SelectMany(GetIds) @@ -16,8 +18,7 @@ public class Day2 : Solution public override string SolvePart2() { - return InputReader - .ReadLines(2) + return input.Lines .SelectMany(ToRanges) .Select(ToLimit) .SelectMany(GetIds) diff --git a/AdventOfCode2025/Day3.cs b/AdventOfCode2025/Day3.cs index bda7844..759961c 100644 --- a/AdventOfCode2025/Day3.cs +++ b/AdventOfCode2025/Day3.cs @@ -1,31 +1,23 @@ +using AdventOfCode2025.Inputs; + namespace AdventOfCode2025; public class Day3 : Solution { - public override string SolvePart1() => - InputReader - .ReadLines(3) - .Select(line => line.Select(c => c - '0').ToArray()) - .Select(bank => GetLargestJolts(bank)) - .Sum() - .ToString(); + private readonly Input input = new(3); - public override string SolvePart2() => - InputReader - .ReadLines(3) - .Select(line => line.Select(c => c - '0').ToArray()) - .Select(bank => GetLargestJolts(bank, 12)) - .Sum() - .ToString(); + public override string SolvePart1() => input.GetLines(x => ParseLine(x, 2)).Sum().ToString(); + + public override string SolvePart2() => input.GetLines(x => ParseLine(x, 12)).Sum().ToString(); private static long GetLargestJolts(int[] bank, int numberOfJolts = 2) { var total = 0L; var from = 0; - var to = bank.Length - numberOfJolts; - for (var i = numberOfJolts - 1; i >= 0; i--) + int to = bank.Length - numberOfJolts; + for (int i = numberOfJolts - 1; i >= 0; i--) { - var maxIndexI = GetIndexOfMax(bank, from, to); + int maxIndexI = GetIndexOfMax(bank, from, to); total += bank[maxIndexI] * (long)Math.Pow(10, i); from = maxIndexI + 1; to = bank.Length - i; @@ -44,13 +36,13 @@ public class Day3 : Solution private static int GetIndexOfMax(int[] array, int from, int to) { - var maxIndex = from; + int maxIndex = from; if (from + 1 > to) { return maxIndex; } - for (var i = from + 1; i <= to; i++) + for (int i = from + 1; i <= to; i++) { if (array[i] > array[maxIndex]) { @@ -60,4 +52,10 @@ public class Day3 : Solution return maxIndex; } + + private static long ParseLine(string line, int numberOfJolts) + { + int[] bank = line.Select(c => c - '0').ToArray(); + return GetLargestJolts(bank, numberOfJolts); + } } \ No newline at end of file diff --git a/AdventOfCode2025/Day4.cs b/AdventOfCode2025/Day4.cs index 94a24e6..d36a791 100644 --- a/AdventOfCode2025/Day4.cs +++ b/AdventOfCode2025/Day4.cs @@ -1,13 +1,16 @@ +using AdventOfCode2025.Inputs; + namespace AdventOfCode2025; public class Day4 : Solution { + private readonly Input input = new(4); private const char NoneChar = '.'; private const char PaperChar = '@'; public override string SolvePart1() { - var grid = PaddingField(InputReader.ReadLines(4)) + var grid = PaddingField(input.Lines) .Select(line => line.ToCharArray()) .ToArray(); @@ -16,7 +19,7 @@ public class Day4 : Solution public override string SolvePart2() { - var grid = PaddingField(InputReader.ReadLines(4)) + var grid = PaddingField(input.Lines) .Select(line => line.ToCharArray()) .ToArray(); List<(int, int)> paperCanBeRemoved; diff --git a/AdventOfCode2025/Day5.cs b/AdventOfCode2025/Day5.cs index 6f72738..4abca4d 100644 --- a/AdventOfCode2025/Day5.cs +++ b/AdventOfCode2025/Day5.cs @@ -1,10 +1,13 @@ +using AdventOfCode2025.Inputs; + namespace AdventOfCode2025; public class Day5 : Solution { + private readonly Input input = new(5); public override string SolvePart1() { - List lines = InputReader.ReadLines(5).ToList(); + List lines = input.Lines.ToList(); List freshRanges = GetFreshRanges(lines); int freshIngredients = lines[(freshRanges.Count + 1)..].Select(long.Parse).Count(id => IsFresh(id, freshRanges)); return freshIngredients.ToString(); @@ -12,7 +15,7 @@ public class Day5 : Solution public override string SolvePart2() { - List lines = InputReader.ReadLines(5).ToList(); + List lines = input.Lines.ToList(); List ranges = GetFreshRanges(lines).OrderBy(x => x.From).ThenBy(x => x.To).ToList(); var merged = new List(); diff --git a/AdventOfCode2025/InputReader.cs b/AdventOfCode2025/InputReader.cs deleted file mode 100644 index 36e8973..0000000 --- a/AdventOfCode2025/InputReader.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace AdventOfCode2025; - -public static class InputReader -{ - private const string BasePath = "../../../Inputs/"; - public static IEnumerable ReadLines(int day, int partNumber = 1) => File.ReadAllLines($"{BasePath}Day{day}_{partNumber}.txt"); -} \ No newline at end of file diff --git a/AdventOfCode2025/Inputs/Input.cs b/AdventOfCode2025/Inputs/Input.cs new file mode 100644 index 0000000..9a3b71e --- /dev/null +++ b/AdventOfCode2025/Inputs/Input.cs @@ -0,0 +1,16 @@ +namespace AdventOfCode2025.Inputs; + +public sealed record Input(int Day, int PartNumber = 1) +{ + private const string BasePath = "../../../Inputs/"; + public string ToPath() => BasePath + $"Day{Day}_{PartNumber}.txt"; +} + +public static class InputExtensions +{ + extension(Input input) + { + public IEnumerable Lines => File.ReadAllLines(input.ToPath()); + public IEnumerable GetLines(Func parser) => input.Lines.Select(parser); + } +} \ No newline at end of file