refactor: Move Input to the base class

This commit is contained in:
Sithis 2025-12-07 10:07:24 +01:00
parent 22110d8870
commit 8fc5668e60
7 changed files with 50 additions and 44 deletions

View file

@ -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,

View file

@ -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<long> 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]);
}

View file

@ -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)
{

View file

@ -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<string> PaddingField(IEnumerable<string> field)
{
List<string> 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);

View file

@ -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<string> lines = input.Lines.ToList();
List<string> lines = Input.Lines.ToList();
List<Range> 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<string> lines = input.Lines.ToList();
List<string> lines = Input.Lines.ToList();
List<Range> ranges = GetFreshRanges(lines).OrderBy(x => x.From).ThenBy(x => x.To).ToList();
var merged = new List<Range>();

View file

@ -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<string> lines = input.Lines.ToList();
List<string> lines = Input.Lines.ToList();
int numberOfNumberRows = lines.Count - 1;
var numbersOfProblem = new List<string[]>();
var currentNumberRows = new string[numberOfNumberRows];

View file

@ -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";
}