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;
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
public sealed class Day1 : Solution
|
public sealed class Day1 : Solution
|
||||||
{
|
{
|
||||||
|
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;
|
||||||
IEnumerable<string> lines = InputReader.ReadLines(1);
|
foreach (var line in input.Lines)
|
||||||
foreach (var line in lines)
|
|
||||||
{
|
{
|
||||||
var number = int.Parse(line[1..]);
|
var number = int.Parse(line[1..]);
|
||||||
arrow = line.First() switch
|
arrow = line.First() switch
|
||||||
|
|
@ -33,9 +35,8 @@ public sealed class Day1 : Solution
|
||||||
{
|
{
|
||||||
var arrow = 50;
|
var arrow = 50;
|
||||||
var zeroCounter = 0;
|
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..]);
|
var number = int.Parse(line[1..]);
|
||||||
zeroCounter += number / MaxArrow;
|
zeroCounter += number / MaxArrow;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
|
using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025;
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
public class Day2 : Solution
|
public class Day2 : Solution
|
||||||
{
|
{
|
||||||
|
private readonly Input input = new(2);
|
||||||
public override string SolvePart1()
|
public override string SolvePart1()
|
||||||
{
|
{
|
||||||
return InputReader
|
return input.Lines
|
||||||
.ReadLines(2)
|
|
||||||
.SelectMany(ToRanges)
|
.SelectMany(ToRanges)
|
||||||
.Select(ToLimit)
|
.Select(ToLimit)
|
||||||
.SelectMany(GetIds)
|
.SelectMany(GetIds)
|
||||||
|
|
@ -16,8 +18,7 @@ public class Day2 : Solution
|
||||||
|
|
||||||
public override string SolvePart2()
|
public override string SolvePart2()
|
||||||
{
|
{
|
||||||
return InputReader
|
return input.Lines
|
||||||
.ReadLines(2)
|
|
||||||
.SelectMany(ToRanges)
|
.SelectMany(ToRanges)
|
||||||
.Select(ToLimit)
|
.Select(ToLimit)
|
||||||
.SelectMany(GetIds)
|
.SelectMany(GetIds)
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,23 @@
|
||||||
|
using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025;
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
public class Day3 : Solution
|
public class Day3 : Solution
|
||||||
{
|
{
|
||||||
public override string SolvePart1() =>
|
private readonly Input input = new(3);
|
||||||
InputReader
|
|
||||||
.ReadLines(3)
|
|
||||||
.Select(line => line.Select(c => c - '0').ToArray())
|
|
||||||
.Select(bank => GetLargestJolts(bank))
|
|
||||||
.Sum()
|
|
||||||
.ToString();
|
|
||||||
|
|
||||||
public override string SolvePart2() =>
|
public override string SolvePart1() => input.GetLines(x => ParseLine(x, 2)).Sum().ToString();
|
||||||
InputReader
|
|
||||||
.ReadLines(3)
|
public override string SolvePart2() => input.GetLines(x => ParseLine(x, 12)).Sum().ToString();
|
||||||
.Select(line => line.Select(c => c - '0').ToArray())
|
|
||||||
.Select(bank => GetLargestJolts(bank, 12))
|
|
||||||
.Sum()
|
|
||||||
.ToString();
|
|
||||||
|
|
||||||
private static long GetLargestJolts(int[] bank, int numberOfJolts = 2)
|
private static long GetLargestJolts(int[] bank, int numberOfJolts = 2)
|
||||||
{
|
{
|
||||||
var total = 0L;
|
var total = 0L;
|
||||||
var from = 0;
|
var from = 0;
|
||||||
var to = bank.Length - numberOfJolts;
|
int to = bank.Length - numberOfJolts;
|
||||||
for (var i = numberOfJolts - 1; i >= 0; i--)
|
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);
|
total += bank[maxIndexI] * (long)Math.Pow(10, i);
|
||||||
from = maxIndexI + 1;
|
from = maxIndexI + 1;
|
||||||
to = bank.Length - i;
|
to = bank.Length - i;
|
||||||
|
|
@ -44,13 +36,13 @@ public class Day3 : Solution
|
||||||
|
|
||||||
private static int GetIndexOfMax(int[] array, int from, int to)
|
private static int GetIndexOfMax(int[] array, int from, int to)
|
||||||
{
|
{
|
||||||
var maxIndex = from;
|
int maxIndex = from;
|
||||||
if (from + 1 > to)
|
if (from + 1 > to)
|
||||||
{
|
{
|
||||||
return maxIndex;
|
return maxIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = from + 1; i <= to; i++)
|
for (int i = from + 1; i <= to; i++)
|
||||||
{
|
{
|
||||||
if (array[i] > array[maxIndex])
|
if (array[i] > array[maxIndex])
|
||||||
{
|
{
|
||||||
|
|
@ -60,4 +52,10 @@ public class Day3 : Solution
|
||||||
|
|
||||||
return maxIndex;
|
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;
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
public class Day4 : Solution
|
public class Day4 : Solution
|
||||||
{
|
{
|
||||||
|
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(InputReader.ReadLines(4))
|
var grid = PaddingField(input.Lines)
|
||||||
.Select(line => line.ToCharArray())
|
.Select(line => line.ToCharArray())
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
|
@ -16,7 +19,7 @@ public class Day4 : Solution
|
||||||
|
|
||||||
public override string SolvePart2()
|
public override string SolvePart2()
|
||||||
{
|
{
|
||||||
var grid = PaddingField(InputReader.ReadLines(4))
|
var grid = PaddingField(input.Lines)
|
||||||
.Select(line => line.ToCharArray())
|
.Select(line => line.ToCharArray())
|
||||||
.ToArray();
|
.ToArray();
|
||||||
List<(int, int)> paperCanBeRemoved;
|
List<(int, int)> paperCanBeRemoved;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
|
using AdventOfCode2025.Inputs;
|
||||||
|
|
||||||
namespace AdventOfCode2025;
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
public class Day5 : Solution
|
public class Day5 : Solution
|
||||||
{
|
{
|
||||||
|
private readonly Input input = new(5);
|
||||||
public override string SolvePart1()
|
public override string SolvePart1()
|
||||||
{
|
{
|
||||||
List<string> lines = InputReader.ReadLines(5).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();
|
||||||
|
|
@ -12,7 +15,7 @@ public class Day5 : Solution
|
||||||
|
|
||||||
public override string SolvePart2()
|
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();
|
List<Range> ranges = GetFreshRanges(lines).OrderBy(x => x.From).ThenBy(x => x.To).ToList();
|
||||||
|
|
||||||
var merged = new List<Range>();
|
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