mirror of
https://github.com/parnic/advent-of-code-2022.git
synced 2025-06-16 21:41:53 -05:00
Embed inputs as resources
This enables the binary to be invoked from anywhere and still run on the default input set, as well as being more portable/shareable which can help with performance comparisons between machines. Files are still supported if they exist.
This commit is contained in:
@ -21,78 +21,56 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="inputs\01.txt">
|
<None Remove="inputs\01.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\01.txt" />
|
||||||
</None>
|
<None Remove="inputs\02.txt" />
|
||||||
<None Update="inputs\02.txt">
|
<EmbeddedResource Include="inputs\02.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\03.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\03.txt" />
|
||||||
<None Update="inputs\03.txt">
|
<None Remove="inputs\04.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\04.txt" />
|
||||||
</None>
|
<None Remove="inputs\05.txt" />
|
||||||
<None Update="inputs\05.txt">
|
<EmbeddedResource Include="inputs\05.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\06.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\06.txt" />
|
||||||
<None Update="inputs\06.txt">
|
<None Remove="inputs\07.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\07.txt" />
|
||||||
</None>
|
<None Remove="inputs\08.txt" />
|
||||||
<None Update="inputs\07.txt">
|
<EmbeddedResource Include="inputs\08.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\09.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\09.txt" />
|
||||||
<None Update="inputs\08.txt">
|
<None Remove="inputs\10.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\10.txt" />
|
||||||
</None>
|
<None Remove="inputs\11.txt" />
|
||||||
<None Update="inputs\09.txt">
|
<EmbeddedResource Include="inputs\11.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\12.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\12.txt" />
|
||||||
<None Update="inputs\10.txt">
|
<None Remove="inputs\13.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\13.txt" />
|
||||||
</None>
|
<None Remove="inputs\14.txt" />
|
||||||
<None Update="inputs\11.txt">
|
<EmbeddedResource Include="inputs\14.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\15.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\15.txt" />
|
||||||
<None Update="inputs\12.txt">
|
<None Remove="inputs\16.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\16.txt" />
|
||||||
</None>
|
<None Remove="inputs\17.txt" />
|
||||||
<None Update="inputs\13.txt">
|
<EmbeddedResource Include="inputs\17.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\18.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\18.txt" />
|
||||||
<None Update="inputs\14.txt">
|
<None Remove="inputs\19.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\19.txt" />
|
||||||
</None>
|
<None Remove="inputs\20.txt" />
|
||||||
<None Update="inputs\15.txt">
|
<EmbeddedResource Include="inputs\20.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\21.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\21.txt" />
|
||||||
<None Update="inputs\16.txt">
|
<None Remove="inputs\22.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\22.txt" />
|
||||||
</None>
|
<None Remove="inputs\23.txt" />
|
||||||
<None Update="inputs\17.txt">
|
<EmbeddedResource Include="inputs\23.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<None Remove="inputs\24.txt" />
|
||||||
</None>
|
<EmbeddedResource Include="inputs\24.txt" />
|
||||||
<None Update="inputs\18.txt">
|
<None Remove="inputs\25.txt" />
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<EmbeddedResource Include="inputs\25.txt" />
|
||||||
</None>
|
|
||||||
<None Update="inputs\19.txt">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
<None Update="inputs\20.txt">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
<None Update="inputs\21.txt">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
<None Update="inputs\22.txt">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
<None Update="inputs\23.txt">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
<None Update="inputs\24.txt">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
<None Update="inputs\25.txt">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -6,7 +6,7 @@ internal class Day01 : Day
|
|||||||
|
|
||||||
internal override void Parse()
|
internal override void Parse()
|
||||||
{
|
{
|
||||||
lines = Util.ReadAllLines("inputs/01.txt");
|
lines = Util.ReadAllLines("01");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override string Part1()
|
internal override string Part1()
|
||||||
|
@ -6,7 +6,7 @@ internal class DayTemplate : Day
|
|||||||
|
|
||||||
internal override void Parse()
|
internal override void Parse()
|
||||||
{
|
{
|
||||||
lines = Util.ReadAllLines("inputs/##.txt");
|
lines = Util.ReadAllLines("##");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override string Part1()
|
internal override string Part1()
|
||||||
|
22
src/Util.cs
22
src/Util.cs
@ -1,4 +1,5 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace aoc2022;
|
namespace aoc2022;
|
||||||
@ -6,7 +7,7 @@ namespace aoc2022;
|
|||||||
internal static class Util
|
internal static class Util
|
||||||
{
|
{
|
||||||
private static readonly char[] StripPreamble = new char[] { (char)8745, (char)9559, (char)9488, };
|
private static readonly char[] StripPreamble = new char[] { (char)8745, (char)9559, (char)9488, };
|
||||||
internal static void ReadData(string filename, Action<string> processor)
|
internal static void ReadData(string inputName, Action<string> processor)
|
||||||
{
|
{
|
||||||
if (Console.IsInputRedirected)
|
if (Console.IsInputRedirected)
|
||||||
{
|
{
|
||||||
@ -39,9 +40,24 @@ internal static class Util
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var line in File.ReadLines(filename))
|
var filename = $"inputs/{inputName}.txt";
|
||||||
|
if (File.Exists(filename))
|
||||||
{
|
{
|
||||||
processor(line);
|
foreach (var line in File.ReadLines(filename))
|
||||||
|
{
|
||||||
|
processor(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// typeof(Util) is not technically correct since what we need is the "default namespace,"
|
||||||
|
// but "default namespace" is a Project File concept, not a C#/.NET concept, so it's not
|
||||||
|
// accessible at runtime. instead, we assume Util is also part of the "default namespace"
|
||||||
|
var resourceName = $"{typeof(Util).Namespace}.inputs.{inputName}.txt";
|
||||||
|
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
|
||||||
|
using StreamReader reader = new(stream!);
|
||||||
|
while (reader.ReadLine() is { } readLine)
|
||||||
|
{
|
||||||
|
processor(readLine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user