Initial Commit
This commit is contained in:
9
2024/AdventCommon/AdventCommon.csproj
Normal file
9
2024/AdventCommon/AdventCommon.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
31
2024/AdventCommon/ParsedInput.cs
Normal file
31
2024/AdventCommon/ParsedInput.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AdventCommon
|
||||
{
|
||||
public abstract class ParsedInput
|
||||
{
|
||||
public virtual object? GetContext() { return null; }
|
||||
|
||||
public abstract bool ParseLine(string line, object? context = null);
|
||||
|
||||
public virtual long GetPart01() { return 0; }
|
||||
public virtual long GetPart02() { return 0; }
|
||||
|
||||
public ParsedInput(string fileName)
|
||||
{
|
||||
var input = new AdventCommon.PuzzleInput(fileName);
|
||||
|
||||
foreach (var line in input.Lines)
|
||||
{
|
||||
if (!ParseLine(line, GetContext()))
|
||||
{
|
||||
throw new Exception("Line not formatted as expected");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
2024/AdventCommon/PuzzleInput.cs
Normal file
35
2024/AdventCommon/PuzzleInput.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace AdventCommon
|
||||
{
|
||||
public class PuzzleInput
|
||||
{
|
||||
public PuzzleInput(string fileName, bool ignoreEmptyLines = true)
|
||||
{
|
||||
using (StreamReader reader = System.IO.File.OpenText(fileName))
|
||||
{
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
string? line = reader.ReadLine();
|
||||
|
||||
if (line == null) { throw new InvalidDataException(); }
|
||||
if (ignoreEmptyLines && String.IsNullOrWhiteSpace(line)) continue;
|
||||
|
||||
Lines.Add(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> Lines { get; private set; } = new List<string>();
|
||||
|
||||
public void Print()
|
||||
{
|
||||
for (int j = 0; j < Lines.Count; j++)
|
||||
{
|
||||
for (int i = 0; i < Lines[j].Length; i++)
|
||||
{
|
||||
Console.Write(Lines[j][i]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user