refactor: Change Input loading to class instead of reader
This commit is contained in:
parent
1a4e35b61c
commit
3723115a4a
7 changed files with 53 additions and 38 deletions
|
|
@ -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<string> 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<string> lines = InputReader.ReadLines(1);
|
||||
|
||||
foreach (var line in lines)
|
||||
foreach (var line in input.Lines)
|
||||
{
|
||||
var number = int.Parse(line[1..]);
|
||||
zeroCounter += number / MaxArrow;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
using AdventOfCode2025.Inputs;
|
||||
|
||||
namespace AdventOfCode2025;
|
||||
|
||||
public class Day5 : Solution
|
||||
{
|
||||
private readonly Input input = new(5);
|
||||
public override string SolvePart1()
|
||||
{
|
||||
List<string> lines = InputReader.ReadLines(5).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();
|
||||
|
|
@ -12,7 +15,7 @@ public class Day5 : Solution
|
|||
|
||||
public override string SolvePart2()
|
||||
{
|
||||
List<string> lines = InputReader.ReadLines(5).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>();
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
namespace AdventOfCode2025;
|
||||
|
||||
public static class InputReader
|
||||
{
|
||||
private const string BasePath = "../../../Inputs/";
|
||||
public static IEnumerable<string> ReadLines(int day, int partNumber = 1) => File.ReadAllLines($"{BasePath}Day{day}_{partNumber}.txt");
|
||||
}
|
||||
16
AdventOfCode2025/Inputs/Input.cs
Normal file
16
AdventOfCode2025/Inputs/Input.cs
Normal file
|
|
@ -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<string> Lines => File.ReadAllLines(input.ToPath());
|
||||
public IEnumerable<T> GetLines<T>(Func<string, T> parser) => input.Lines.Select(parser);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue