refactor: Move Input to the base class
This commit is contained in:
parent
22110d8870
commit
8fc5668e60
7 changed files with 50 additions and 44 deletions
|
|
@ -2,18 +2,17 @@ using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025.Solutions;
|
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;
|
private const int MaxArrow = 100;
|
||||||
|
|
||||||
public override string SolvePart1()
|
public override string SolvePart1()
|
||||||
{
|
{
|
||||||
var arrow = 50;
|
var arrow = 50;
|
||||||
var zeroCounter = 0;
|
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
|
arrow = line.First() switch
|
||||||
{
|
{
|
||||||
'L' => arrow - number,
|
'L' => arrow - number,
|
||||||
|
|
@ -36,13 +35,13 @@ public sealed class Day1 : Solution
|
||||||
var arrow = 50;
|
var arrow = 50;
|
||||||
var zeroCounter = 0;
|
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;
|
zeroCounter += number / MaxArrow;
|
||||||
var remainingNumber = number % MaxArrow;
|
int remainingNumber = number % MaxArrow;
|
||||||
|
|
||||||
var end = line.First() switch
|
int end = line.First() switch
|
||||||
{
|
{
|
||||||
'L' => arrow - remainingNumber,
|
'L' => arrow - remainingNumber,
|
||||||
'R' => arrow + remainingNumber,
|
'R' => arrow + remainingNumber,
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,11 @@ using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025.Solutions;
|
namespace AdventOfCode2025.Solutions;
|
||||||
|
|
||||||
public class Day2 : Solution
|
public class Day2() : Solution(2)
|
||||||
{
|
{
|
||||||
private readonly Input input = new(2);
|
|
||||||
public override string SolvePart1()
|
public override string SolvePart1()
|
||||||
{
|
{
|
||||||
return input.Lines
|
return Input.Lines
|
||||||
.SelectMany(ToRanges)
|
.SelectMany(ToRanges)
|
||||||
.Select(ToLimit)
|
.Select(ToLimit)
|
||||||
.SelectMany(GetIds)
|
.SelectMany(GetIds)
|
||||||
|
|
@ -18,7 +17,7 @@ public class Day2 : Solution
|
||||||
|
|
||||||
public override string SolvePart2()
|
public override string SolvePart2()
|
||||||
{
|
{
|
||||||
return input.Lines
|
return Input.Lines
|
||||||
.SelectMany(ToRanges)
|
.SelectMany(ToRanges)
|
||||||
.Select(ToLimit)
|
.Select(ToLimit)
|
||||||
.SelectMany(GetIds)
|
.SelectMany(GetIds)
|
||||||
|
|
@ -31,8 +30,8 @@ public class Day2 : Solution
|
||||||
|
|
||||||
private static IEnumerable<long> GetIds(string from, string to)
|
private static IEnumerable<long> GetIds(string from, string to)
|
||||||
{
|
{
|
||||||
var current = long.Parse(from);
|
long current = long.Parse(from);
|
||||||
var end = long.Parse(to);
|
long end = long.Parse(to);
|
||||||
while (current <= end)
|
while (current <= end)
|
||||||
{
|
{
|
||||||
yield return current;
|
yield return current;
|
||||||
|
|
@ -54,8 +53,8 @@ public class Day2 : Solution
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var testBlock = input[..subLength];
|
string testBlock = input[..subLength];
|
||||||
var theoreticalString = string.Concat(Enumerable.Repeat(testBlock, input.Length / subLength));
|
string theoreticalString = string.Concat(Enumerable.Repeat(testBlock, input.Length / subLength));
|
||||||
if (theoreticalString == input)
|
if (theoreticalString == input)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -78,7 +77,7 @@ public class Day2 : Solution
|
||||||
}
|
}
|
||||||
|
|
||||||
var firstHalfIndex = 0;
|
var firstHalfIndex = 0;
|
||||||
var secondHalfIndex = input.Length / 2;
|
int secondHalfIndex = input.Length / 2;
|
||||||
while (secondHalfIndex < input.Length)
|
while (secondHalfIndex < input.Length)
|
||||||
{
|
{
|
||||||
if (input[firstHalfIndex] != input[secondHalfIndex])
|
if (input[firstHalfIndex] != input[secondHalfIndex])
|
||||||
|
|
@ -95,7 +94,7 @@ public class Day2 : Solution
|
||||||
|
|
||||||
private static (string from, string to) ToLimit(string range)
|
private static (string from, string to) ToLimit(string range)
|
||||||
{
|
{
|
||||||
var parts = range.Split('-');
|
string[] parts = range.Split('-');
|
||||||
return (parts[0], parts[1]);
|
return (parts[0], parts[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,11 @@ using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025.Solutions;
|
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)
|
private static long GetLargestJolts(int[] bank, int numberOfJolts = 2)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,14 @@ using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025.Solutions;
|
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 NoneChar = '.';
|
||||||
private const char PaperChar = '@';
|
private const char PaperChar = '@';
|
||||||
|
|
||||||
public override string SolvePart1()
|
public override string SolvePart1()
|
||||||
{
|
{
|
||||||
var grid = PaddingField(input.Lines)
|
char[][] grid = PaddingField(Input.Lines)
|
||||||
.Select(line => line.ToCharArray())
|
.Select(line => line.ToCharArray())
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
|
@ -19,7 +18,7 @@ public class Day4 : Solution
|
||||||
|
|
||||||
public override string SolvePart2()
|
public override string SolvePart2()
|
||||||
{
|
{
|
||||||
var grid = PaddingField(input.Lines)
|
char[][] grid = PaddingField(Input.Lines)
|
||||||
.Select(line => line.ToCharArray())
|
.Select(line => line.ToCharArray())
|
||||||
.ToArray();
|
.ToArray();
|
||||||
List<(int, int)> paperCanBeRemoved;
|
List<(int, int)> paperCanBeRemoved;
|
||||||
|
|
@ -28,11 +27,12 @@ public class Day4 : Solution
|
||||||
{
|
{
|
||||||
paperCanBeRemoved = GetRemovablePaper(grid).ToList();
|
paperCanBeRemoved = GetRemovablePaper(grid).ToList();
|
||||||
paperCount += paperCanBeRemoved.Count;
|
paperCount += paperCanBeRemoved.Count;
|
||||||
foreach (var (row, col) in paperCanBeRemoved)
|
foreach ((int row, int col) in paperCanBeRemoved)
|
||||||
{
|
{
|
||||||
grid[row][col] = NoneChar;
|
grid[row][col] = NoneChar;
|
||||||
}
|
}
|
||||||
} while (paperCanBeRemoved.Count > 0);
|
}
|
||||||
|
while (paperCanBeRemoved.Count > 0);
|
||||||
|
|
||||||
return paperCount.ToString();
|
return paperCount.ToString();
|
||||||
}
|
}
|
||||||
|
|
@ -59,12 +59,22 @@ public class Day4 : Solution
|
||||||
|
|
||||||
private static bool HasMorePaperNeighborsAs(int x, int y, int threshold, char[][] grid)
|
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 directions = new (int dx, int dy)[]
|
||||||
var paperNeighbors = 0;
|
|
||||||
foreach (var (dx, dy) in directions)
|
|
||||||
{
|
{
|
||||||
var nx = x + dx;
|
(-1, -1),
|
||||||
var ny = y + dy;
|
(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)
|
if (nx < 0 || nx >= grid[0].Length || ny < 0 || ny >= grid.Length)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -88,7 +98,7 @@ public class Day4 : Solution
|
||||||
private IEnumerable<string> PaddingField(IEnumerable<string> field)
|
private IEnumerable<string> PaddingField(IEnumerable<string> field)
|
||||||
{
|
{
|
||||||
List<string> lines = field.ToList();
|
List<string> lines = field.ToList();
|
||||||
var width = lines.First().Length;
|
int width = lines.First().Length;
|
||||||
lines.Insert(0, new string(NoneChar, width));
|
lines.Insert(0, new string(NoneChar, width));
|
||||||
lines.Add(new string(NoneChar, width));
|
lines.Add(new string(NoneChar, width));
|
||||||
return lines.Select(l => NoneChar + l + NoneChar);
|
return lines.Select(l => NoneChar + l + NoneChar);
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,11 @@ using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025.Solutions;
|
namespace AdventOfCode2025.Solutions;
|
||||||
|
|
||||||
public class Day5 : Solution
|
public class Day5() : Solution(5)
|
||||||
{
|
{
|
||||||
private readonly Input input = new(5);
|
|
||||||
public override string SolvePart1()
|
public override string SolvePart1()
|
||||||
{
|
{
|
||||||
List<string> lines = input.Lines.ToList();
|
List<string> lines = Input.Lines.ToList();
|
||||||
List<Range> freshRanges = GetFreshRanges(lines);
|
List<Range> freshRanges = GetFreshRanges(lines);
|
||||||
int freshIngredients = lines[(freshRanges.Count + 1)..].Select(long.Parse).Count(id => IsFresh(id, freshRanges));
|
int freshIngredients = lines[(freshRanges.Count + 1)..].Select(long.Parse).Count(id => IsFresh(id, freshRanges));
|
||||||
return freshIngredients.ToString();
|
return freshIngredients.ToString();
|
||||||
|
|
@ -15,7 +14,7 @@ public class Day5 : Solution
|
||||||
|
|
||||||
public override string SolvePart2()
|
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();
|
List<Range> ranges = GetFreshRanges(lines).OrderBy(x => x.From).ThenBy(x => x.To).ToList();
|
||||||
|
|
||||||
var merged = new List<Range>();
|
var merged = new List<Range>();
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,8 @@ using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025.Solutions;
|
namespace AdventOfCode2025.Solutions;
|
||||||
|
|
||||||
public class Day6 : Solution
|
public class Day6() : Solution(6)
|
||||||
{
|
{
|
||||||
private readonly Input input = new(6);
|
|
||||||
|
|
||||||
public override string SolvePart1()
|
public override string SolvePart1()
|
||||||
{
|
{
|
||||||
(string[][] numberOfProblems, string[] arithmetics) = GetProblems();
|
(string[][] numberOfProblems, string[] arithmetics) = GetProblems();
|
||||||
|
|
@ -51,7 +49,7 @@ public class Day6 : Solution
|
||||||
|
|
||||||
private (string[][] numbersOfProblem, string[] arithmetic) GetProblems()
|
private (string[][] numbersOfProblem, string[] arithmetic) GetProblems()
|
||||||
{
|
{
|
||||||
List<string> lines = input.Lines.ToList();
|
List<string> lines = Input.Lines.ToList();
|
||||||
int numberOfNumberRows = lines.Count - 1;
|
int numberOfNumberRows = lines.Count - 1;
|
||||||
var numbersOfProblem = new List<string[]>();
|
var numbersOfProblem = new List<string[]>();
|
||||||
var currentNumberRows = new string[numberOfNumberRows];
|
var currentNumberRows = new string[numberOfNumberRows];
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
|
using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025.Solutions;
|
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 SolvePart1() => "Part1 is not solved";
|
||||||
public virtual string SolvePart2() => "Part2 is not solved";
|
public virtual string SolvePart2() => "Part2 is not solved";
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue