79 lines
2.5 KiB
C#
79 lines
2.5 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[]> ReadAsArraytString()
|
|
{
|
|
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 Task<Grid<T>> ReadToGrid<T>() where T : Node, new()
|
|
{
|
|
Grid<T> result = new();
|
|
int row = 0;
|
|
await foreach(string line in ReadAsStringLine())
|
|
{
|
|
int charIndex = 0;
|
|
// create the nodes from the lines
|
|
result.DataGrid.AddRange(line.Select(c => new T { X = charIndex++, Y = row, Char = c }));
|
|
row++;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|