90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using AdventOfCode.Core.Shared;
|
|
|
|
namespace AdventOfCode.Core
|
|
{
|
|
public class InputReader
|
|
{
|
|
private readonly string InputFileTemplate = "../../../../AdventOfCode.Solutions/{1}/Day {0:00}/day-{0:00}-input.txt";
|
|
private readonly string DebugInputFileTemplate = "../../../../AdventOfCode.Solutions/day-00-input.txt";
|
|
public bool IsDebug = false;
|
|
|
|
private int Day { get; set; }
|
|
private int Year { get; set; }
|
|
private string InputFilePath => IsDebug ? DebugInputFileTemplate : string.Format(InputFileTemplate, Day, Year);
|
|
|
|
public InputReader() => SetInputByChallange();
|
|
|
|
public void SetInputByChallange()
|
|
{
|
|
SetInputByChallange(DateTime.Now.Day, DateTime.Now.Year);
|
|
}
|
|
|
|
public void SetInputByChallange(int day)
|
|
{
|
|
SetInputByChallange(day, DateTime.Now.Year);
|
|
}
|
|
|
|
public void SetInputByChallange(int day, int year)
|
|
{
|
|
Day = day;
|
|
Year = year;
|
|
}
|
|
|
|
public async Task<string> ReadAsString() => await File.ReadAllTextAsync(InputFilePath);
|
|
|
|
public async IAsyncEnumerable<string> ReadAsStringLine()
|
|
{
|
|
using StreamReader reader = new(InputFilePath);
|
|
while (!reader.EndOfStream)
|
|
{
|
|
yield return (await reader.ReadLineAsync()) ?? string.Empty;
|
|
}
|
|
}
|
|
|
|
public Task<string[]> ReadAsArrayString()
|
|
{
|
|
return File.ReadAllLinesAsync(InputFilePath);
|
|
}
|
|
|
|
public async IAsyncEnumerable<T> ReadAsStringLine<T>(T emptyLineIndicator)
|
|
{
|
|
using StreamReader reader = new(InputFilePath);
|
|
while (!reader.EndOfStream)
|
|
{
|
|
string line = (await reader.ReadLineAsync()) ?? string.Empty;
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
yield return emptyLineIndicator;
|
|
else
|
|
yield return (T)Convert.ChangeType(line, typeof(T));
|
|
}
|
|
}
|
|
|
|
public async IAsyncEnumerable<long[]> ReadLineAsLongArray(string seperator)
|
|
{
|
|
using StreamReader reader = new(InputFilePath);
|
|
while (!reader.EndOfStream)
|
|
{
|
|
string line = (await reader.ReadLineAsync()) ?? string.Empty;
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
yield return [];
|
|
else
|
|
yield return line.Split(seperator).Select(long.Parse).ToArray();
|
|
}
|
|
}
|
|
|
|
public async Task<Grid<T>> ReadToGrid<T>() where T : Node, new()
|
|
{
|
|
Grid<T> result = new();
|
|
int row = 0;
|
|
await foreach(string line in ReadAsStringLine())
|
|
{
|
|
// create the nodes from the lines
|
|
result.DataGrid.AddRange(line.Select((c, i) => new T { X = i, Y = row, Char = c }));
|
|
row++;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|