From 55f8a1806c61cd458e642154879ef0e603900afc Mon Sep 17 00:00:00 2001 From: Parnic Date: Wed, 30 Nov 2022 20:37:36 -0600 Subject: [PATCH] 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. --- advent-of-code-2022.csproj | 122 +++++++++++++++---------------------- src/01.cs | 2 +- src/Template.cs | 2 +- src/Util.cs | 22 ++++++- 4 files changed, 71 insertions(+), 77 deletions(-) diff --git a/advent-of-code-2022.csproj b/advent-of-code-2022.csproj index 32a02d1..0279458 100644 --- a/advent-of-code-2022.csproj +++ b/advent-of-code-2022.csproj @@ -21,78 +21,56 @@ - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/01.cs b/src/01.cs index 2049fa3..43bce61 100644 --- a/src/01.cs +++ b/src/01.cs @@ -6,7 +6,7 @@ internal class Day01 : Day internal override void Parse() { - lines = Util.ReadAllLines("inputs/01.txt"); + lines = Util.ReadAllLines("01"); } internal override string Part1() diff --git a/src/Template.cs b/src/Template.cs index 6e718e4..577109c 100644 --- a/src/Template.cs +++ b/src/Template.cs @@ -6,7 +6,7 @@ internal class DayTemplate : Day internal override void Parse() { - lines = Util.ReadAllLines("inputs/##.txt"); + lines = Util.ReadAllLines("##"); } internal override string Part1() diff --git a/src/Util.cs b/src/Util.cs index 8a8dc23..1e96af5 100644 --- a/src/Util.cs +++ b/src/Util.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Reflection; using System.Text; namespace aoc2022; @@ -6,7 +7,7 @@ namespace aoc2022; internal static class Util { private static readonly char[] StripPreamble = new char[] { (char)8745, (char)9559, (char)9488, }; - internal static void ReadData(string filename, Action processor) + internal static void ReadData(string inputName, Action processor) { 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); } }