Add ability to pipe input from a file

I'm sure there's a better way to strip the UTF-8 BOM from the beginning of the file (and/or use that information to actually handle the string appropriately) but this works for my use case. At this point, we support ASCII and UTF-8 in LE order with or without the byte-order mark and, I think, either \n or \n\r line endings as input text file formats.
This commit is contained in:
2021-12-13 09:46:32 -06:00
parent f2e8a3a288
commit ddf758951e
12 changed files with 43 additions and 11 deletions

View File

@ -4,7 +4,7 @@ internal class Day01 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/01.txt"); var lines = Util.ReadAllLines("inputs/01.txt");
Part1(lines); Part1(lines);
Part2(lines); Part2(lines);
} }

View File

@ -4,7 +4,7 @@ internal class Day02 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/02.txt"); var lines = Util.ReadAllLines("inputs/02.txt");
var instructions = new List<Instruction>(); var instructions = new List<Instruction>();
foreach (var instruction in lines) foreach (var instruction in lines)
{ {

View File

@ -4,7 +4,7 @@ internal class Day03 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/03.txt"); var lines = Util.ReadAllLines("inputs/03.txt");
Part1(lines); Part1(lines);
Part2(lines); Part2(lines);
} }

View File

@ -43,7 +43,7 @@ internal class Day05 : Day
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/05.txt"); var lines = Util.ReadAllLines("inputs/05.txt");
List<Line> segments = new(); List<Line> segments = new();
foreach (var line in lines) foreach (var line in lines)
{ {

View File

@ -4,7 +4,7 @@ internal class Day08 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/08.txt"); var lines = Util.ReadAllLines("inputs/08.txt");
List<(List<string>, List<string>)> puzzle = new(); List<(List<string>, List<string>)> puzzle = new();
foreach (var line in lines) foreach (var line in lines)
{ {

View File

@ -4,7 +4,7 @@ internal class Day09 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/09.txt"); var lines = Util.ReadAllLines("inputs/09.txt").ToArray();
byte[,] grid = new byte[lines.Length, lines[0].Length]; byte[,] grid = new byte[lines.Length, lines[0].Length];
for (int i = 0; i < lines.Length; i++) for (int i = 0; i < lines.Length; i++)
{ {

View File

@ -4,7 +4,7 @@ internal class Day10 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/10.txt"); var lines = Util.ReadAllLines("inputs/10.txt");
Part1(lines); Part1(lines);
Part2(lines); Part2(lines);
} }

View File

@ -4,7 +4,7 @@ internal class Day11 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/11.txt"); var lines = Util.ReadAllLines("inputs/11.txt").ToArray();
var grid = new byte[lines.Length, lines[0].Length]; var grid = new byte[lines.Length, lines[0].Length];
for (int i = 0; i < lines.Length; i++) for (int i = 0; i < lines.Length; i++)
{ {

View File

@ -4,7 +4,7 @@ internal class Day12 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/12.txt"); var lines = Util.ReadAllLines("inputs/12.txt");
var paths = new Dictionary<string, List<string>>(); var paths = new Dictionary<string, List<string>>();
foreach (var line in lines) foreach (var line in lines)
{ {

View File

@ -4,7 +4,7 @@ internal class Day13 : Day
{ {
internal override void Go() internal override void Go()
{ {
var lines = File.ReadAllLines("inputs/13.txt"); var lines = Util.ReadAllLines("inputs/13.txt");
int phase = 0; int phase = 0;
var points = new HashSet<(int, int)>(); var points = new HashSet<(int, int)>();
var folds = new List<(char, int)>(); var folds = new List<(char, int)>();

View File

@ -6,7 +6,7 @@ internal class DayTemplate : Day
{ {
Logger.Log("Day #"); Logger.Log("Day #");
Logger.Log("-----"); Logger.Log("-----");
var lines = File.ReadAllLines("inputs/##.txt"); var lines = Util.ReadAllLines("inputs/##.txt");
Part1(lines); Part1(lines);
Part2(lines); Part2(lines);
Logger.Log(""); Logger.Log("");

32
src/Util.cs Normal file
View File

@ -0,0 +1,32 @@
using System.Text;
namespace aoc2021;
internal static class Util
{
internal static IEnumerable<string> ReadAllLines(string filename)
{
if (Console.IsInputRedirected)
{
string? line;
List<string> lines = new();
while ((line = Console.In.ReadLine()) != null)
{
if (!lines.Any())
{
if (line.StartsWith(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble())))
{
line = line[Encoding.UTF8.GetPreamble().Length..];
}
}
lines.Add(line);
}
return lines;
}
else
{
return File.ReadAllLines(filename);
}
}
}