From 8fc5668e60732d69cd5c205e850de82d04e7f8a1 Mon Sep 17 00:00:00 2001 From: Sithis Date: Sun, 7 Dec 2025 10:07:24 +0100 Subject: [PATCH] refactor: Move Input to the base class --- AdventOfCode2025/Solutions/Day1.cs | 15 ++++++------ AdventOfCode2025/Solutions/Day2.cs | 19 +++++++------- AdventOfCode2025/Solutions/Day3.cs | 8 +++--- AdventOfCode2025/Solutions/Day4.cs | 34 +++++++++++++++++--------- AdventOfCode2025/Solutions/Day5.cs | 7 +++--- AdventOfCode2025/Solutions/Day6.cs | 6 ++--- AdventOfCode2025/Solutions/Solution.cs | 5 +++- 7 files changed, 50 insertions(+), 44 deletions(-) diff --git a/AdventOfCode2025/Solutions/Day1.cs b/AdventOfCode2025/Solutions/Day1.cs index 8acc723..c550835 100644 --- a/AdventOfCode2025/Solutions/Day1.cs +++ b/AdventOfCode2025/Solutions/Day1.cs @@ -2,18 +2,17 @@ using AdventOfCode2025.Inputs; namespace AdventOfCode2025.Solutions; -public sealed class Day1 : Solution +public sealed class Day1() : Solution(1) { - private readonly Input input = new(1); private const int MaxArrow = 100; public override string SolvePart1() { var arrow = 50; var zeroCounter = 0; - foreach (var line in input.Lines) + foreach (string line in Input.Lines) { - var number = int.Parse(line[1..]); + int number = int.Parse(line[1..]); arrow = line.First() switch { 'L' => arrow - number, @@ -36,13 +35,13 @@ public sealed class Day1 : Solution var arrow = 50; var zeroCounter = 0; - foreach (var line in input.Lines) + foreach (string line in Input.Lines) { - var number = int.Parse(line[1..]); + int number = int.Parse(line[1..]); zeroCounter += number / MaxArrow; - var remainingNumber = number % MaxArrow; + int remainingNumber = number % MaxArrow; - var end = line.First() switch + int end = line.First() switch { 'L' => arrow - remainingNumber, 'R' => arrow + remainingNumber, diff --git a/AdventOfCode2025/Solutions/Day2.cs b/AdventOfCode2025/Solutions/Day2.cs index fc365dc..41f00e2 100644 --- a/AdventOfCode2025/Solutions/Day2.cs +++ b/AdventOfCode2025/Solutions/Day2.cs @@ -2,12 +2,11 @@ using AdventOfCode2025.Inputs; namespace AdventOfCode2025.Solutions; -public class Day2 : Solution +public class Day2() : Solution(2) { - private readonly Input input = new(2); public override string SolvePart1() { - return input.Lines + return Input.Lines .SelectMany(ToRanges) .Select(ToLimit) .SelectMany(GetIds) @@ -18,7 +17,7 @@ public class Day2 : Solution public override string SolvePart2() { - return input.Lines + return Input.Lines .SelectMany(ToRanges) .Select(ToLimit) .SelectMany(GetIds) @@ -31,8 +30,8 @@ public class Day2 : Solution private static IEnumerable GetIds(string from, string to) { - var current = long.Parse(from); - var end = long.Parse(to); + long current = long.Parse(from); + long end = long.Parse(to); while (current <= end) { yield return current; @@ -54,8 +53,8 @@ public class Day2 : Solution continue; } - var testBlock = input[..subLength]; - var theoreticalString = string.Concat(Enumerable.Repeat(testBlock, input.Length / subLength)); + string testBlock = input[..subLength]; + string theoreticalString = string.Concat(Enumerable.Repeat(testBlock, input.Length / subLength)); if (theoreticalString == input) { return true; @@ -78,7 +77,7 @@ public class Day2 : Solution } var firstHalfIndex = 0; - var secondHalfIndex = input.Length / 2; + int secondHalfIndex = input.Length / 2; while (secondHalfIndex < input.Length) { if (input[firstHalfIndex] != input[secondHalfIndex]) @@ -95,7 +94,7 @@ public class Day2 : Solution private static (string from, string to) ToLimit(string range) { - var parts = range.Split('-'); + string[] parts = range.Split('-'); return (parts[0], parts[1]); } diff --git a/AdventOfCode2025/Solutions/Day3.cs b/AdventOfCode2025/Solutions/Day3.cs index fce7fda..16f6440 100644 --- a/AdventOfCode2025/Solutions/Day3.cs +++ b/AdventOfCode2025/Solutions/Day3.cs @@ -2,13 +2,11 @@ using AdventOfCode2025.Inputs; namespace AdventOfCode2025.Solutions; -public class Day3 : Solution +public class Day3() : Solution(3) { - private readonly Input input = new(3); + public override string SolvePart1() => Input.Lines.Select(x => ParseLine(x, 2)).Sum().ToString(); - public override string SolvePart1() => input.Lines.Select(x => ParseLine(x, 2)).Sum().ToString(); - - public override string SolvePart2() => input.Lines.Select(x => ParseLine(x, 12)).Sum().ToString(); + public override string SolvePart2() => Input.Lines.Select(x => ParseLine(x, 12)).Sum().ToString(); private static long GetLargestJolts(int[] bank, int numberOfJolts = 2) { diff --git a/AdventOfCode2025/Solutions/Day4.cs b/AdventOfCode2025/Solutions/Day4.cs index ffb2299..d02ff72 100644 --- a/AdventOfCode2025/Solutions/Day4.cs +++ b/AdventOfCode2025/Solutions/Day4.cs @@ -2,15 +2,14 @@ using AdventOfCode2025.Inputs; namespace AdventOfCode2025.Solutions; -public class Day4 : Solution +public class Day4() : Solution(4) { - private readonly Input input = new(4); private const char NoneChar = '.'; private const char PaperChar = '@'; public override string SolvePart1() { - var grid = PaddingField(input.Lines) + char[][] grid = PaddingField(Input.Lines) .Select(line => line.ToCharArray()) .ToArray(); @@ -19,7 +18,7 @@ public class Day4 : Solution public override string SolvePart2() { - var grid = PaddingField(input.Lines) + char[][] grid = PaddingField(Input.Lines) .Select(line => line.ToCharArray()) .ToArray(); List<(int, int)> paperCanBeRemoved; @@ -28,11 +27,12 @@ public class Day4 : Solution { paperCanBeRemoved = GetRemovablePaper(grid).ToList(); paperCount += paperCanBeRemoved.Count; - foreach (var (row, col) in paperCanBeRemoved) + foreach ((int row, int col) in paperCanBeRemoved) { grid[row][col] = NoneChar; } - } while (paperCanBeRemoved.Count > 0); + } + while (paperCanBeRemoved.Count > 0); return paperCount.ToString(); } @@ -59,12 +59,22 @@ public class Day4 : Solution private static bool HasMorePaperNeighborsAs(int x, int y, int threshold, char[][] grid) { - var directions = new (int dx, int dy)[] { (-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (0, 1), (-1, 1), (1, 1) }; - var paperNeighbors = 0; - foreach (var (dx, dy) in directions) + var directions = new (int dx, int dy)[] { - var nx = x + dx; - var ny = y + dy; + (-1, -1), + (0, -1), + (1, -1), + (-1, 0), + (1, 0), + (0, 1), + (-1, 1), + (1, 1) + }; + var paperNeighbors = 0; + foreach ((int dx, int dy) in directions) + { + int nx = x + dx; + int ny = y + dy; if (nx < 0 || nx >= grid[0].Length || ny < 0 || ny >= grid.Length) { continue; @@ -88,7 +98,7 @@ public class Day4 : Solution private IEnumerable PaddingField(IEnumerable field) { List lines = field.ToList(); - var width = lines.First().Length; + int width = lines.First().Length; lines.Insert(0, new string(NoneChar, width)); lines.Add(new string(NoneChar, width)); return lines.Select(l => NoneChar + l + NoneChar); diff --git a/AdventOfCode2025/Solutions/Day5.cs b/AdventOfCode2025/Solutions/Day5.cs index 0c1d374..5986c88 100644 --- a/AdventOfCode2025/Solutions/Day5.cs +++ b/AdventOfCode2025/Solutions/Day5.cs @@ -2,12 +2,11 @@ using AdventOfCode2025.Inputs; namespace AdventOfCode2025.Solutions; -public class Day5 : Solution +public class Day5() : Solution(5) { - private readonly Input input = new(5); public override string SolvePart1() { - List lines = input.Lines.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(); @@ -15,7 +14,7 @@ public class Day5 : Solution public override string SolvePart2() { - List lines = input.Lines.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/Solutions/Day6.cs b/AdventOfCode2025/Solutions/Day6.cs index 4185b37..e09e1b8 100644 --- a/AdventOfCode2025/Solutions/Day6.cs +++ b/AdventOfCode2025/Solutions/Day6.cs @@ -2,10 +2,8 @@ using AdventOfCode2025.Inputs; namespace AdventOfCode2025.Solutions; -public class Day6 : Solution +public class Day6() : Solution(6) { - private readonly Input input = new(6); - public override string SolvePart1() { (string[][] numberOfProblems, string[] arithmetics) = GetProblems(); @@ -51,7 +49,7 @@ public class Day6 : Solution private (string[][] numbersOfProblem, string[] arithmetic) GetProblems() { - List lines = input.Lines.ToList(); + List lines = Input.Lines.ToList(); int numberOfNumberRows = lines.Count - 1; var numbersOfProblem = new List(); var currentNumberRows = new string[numberOfNumberRows]; diff --git a/AdventOfCode2025/Solutions/Solution.cs b/AdventOfCode2025/Solutions/Solution.cs index 3a5e684..19d7b69 100644 --- a/AdventOfCode2025/Solutions/Solution.cs +++ b/AdventOfCode2025/Solutions/Solution.cs @@ -1,7 +1,10 @@ +using AdventOfCode2025.Inputs; + namespace AdventOfCode2025.Solutions; -public abstract class Solution +public abstract class Solution(int day, int part = 1) { + protected readonly Input Input = new(day, part); public virtual string SolvePart1() => "Part1 is not solved"; public virtual string SolvePart2() => "Part2 is not solved"; } \ No newline at end of file