Add automatic input downloading

Since we no longer include input files with the solution, per AoC guidelines, this will enable other users to use this application (after specifying their session token) without manually grabbing all the appropriate download files.
This commit is contained in:
2024-12-01 11:13:56 -06:00
parent 8120a52ba0
commit 0359b86174
7 changed files with 184 additions and 42 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
/obj/ /obj/
*.exe *.exe
/inputs/ /inputs/
.env

View File

@ -23,16 +23,6 @@
<NoWarn>1701;1702;8981</NoWarn> <NoWarn>1701;1702;8981</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="inputs\01.txt" />
<None Remove="inputs\01a.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="inputs\01.txt" />
<EmbeddedResource Include="inputs\01a.txt" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Z3" Version="4.12.2" /> <PackageReference Include="Microsoft.Z3" Version="4.12.2" />
</ItemGroup> </ItemGroup>

View File

@ -50,12 +50,24 @@ internal class Logger
Debug.WriteLine(StripColorCodes(msg)); Debug.WriteLine(StripColorCodes(msg));
} }
public static void LogErrorLine(string msg)
{
Console.Error.WriteLine(InsertColorCodes(msg));
Debug.WriteLine(StripColorCodes(msg));
}
public static void Log(string msg) public static void Log(string msg)
{ {
Console.Write(InsertColorCodes(msg)); Console.Write(InsertColorCodes(msg));
Debug.Write(StripColorCodes(msg)); Debug.Write(StripColorCodes(msg));
} }
public static void LogError(string msg)
{
Console.Error.Write(InsertColorCodes(msg));
Debug.Write(StripColorCodes(msg));
}
private static string InsertColorCodes(string msg) private static string InsertColorCodes(string msg)
{ {
foreach (var code in colorCodes) foreach (var code in colorCodes)

65
src/Util/DotEnv.cs Normal file
View File

@ -0,0 +1,65 @@
namespace aoc2024.Util;
internal static class DotEnv
{
internal static string GetDotEnvContents(string fromPath = "")
{
if (string.IsNullOrEmpty(fromPath))
{
fromPath = Directory.GetCurrentDirectory();
}
try
{
var dir = new DirectoryInfo(fromPath);
while (dir != null)
{
var dotEnv = Path.Combine(dir.FullName, ".env");
if (File.Exists(dotEnv))
{
return dotEnv;
}
dir = dir.Parent;
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Exception searching for .env path from {fromPath}: {ex}");
}
return "";
}
internal static bool SetEnvironment(string fromPath = "")
{
var dotEnv = GetDotEnvContents(fromPath);
if (string.IsNullOrEmpty(dotEnv))
{
return false;
}
var lines = File.ReadAllLines(dotEnv);
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i].Trim();
if (string.IsNullOrWhiteSpace(line) || line.StartsWith('#'))
{
continue;
}
var parts = line.Split('=');
if (parts.Length != 2)
{
Logger.LogErrorLine($"DotEnv file {dotEnv} line {i + 1} does not match expected `key=value` format. Line: {line}");
continue;
}
System.Diagnostics.Debug.WriteLine($"Setting environment variable `{parts[0]}` = `{parts[1]}`");
Environment.SetEnvironmentVariable(parts[0], parts[1]);
}
return true;
}
}

View File

@ -44,7 +44,7 @@ public static class Parsing
} }
} }
var filename = $"inputs/{inputName}.txt"; var filename = Path.Combine("inputs", $"{inputName}.txt");
if (File.Exists(filename)) if (File.Exists(filename))
{ {
if (Directory.Exists(Path.GetDirectoryName(filename)!) && File.Exists(filename)) if (Directory.Exists(Path.GetDirectoryName(filename)!) && File.Exists(filename))
@ -63,7 +63,14 @@ public static class Parsing
// accessible at runtime. instead, we assume Logger is also part of the "default namespace" // accessible at runtime. instead, we assume Logger is also part of the "default namespace"
var resourceName = $"{typeof(Logger).Namespace}.inputs.{inputName}.txt"; var resourceName = $"{typeof(Logger).Namespace}.inputs.{inputName}.txt";
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName); using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
using StreamReader reader = new(stream!); if (stream == null)
{
Logger.LogErrorLine($"Unable to find file or resource matching requested input {inputName}.");
Logger.LogErrorLine("Do you have a .env file with an AOC_SESSION=... line containing your session cookie?");
return;
}
using StreamReader reader = new(stream);
while (reader.ReadLine() is { } readLine) while (reader.ReadLine() is { } readLine)
{ {
processor(readLine); processor(readLine);

19
src/Util/RetrieveInput.cs Normal file
View File

@ -0,0 +1,19 @@
using System.Net;
namespace aoc2024.Util;
internal static class RetrieveInput
{
internal static async Task Get(string session, string year, int day)
{
var cookies = new CookieContainer();
cookies.Add(new Uri("https://adventofcode.com"), new Cookie("session", session));
using var handler = new HttpClientHandler() { CookieContainer = cookies };
using var client = new HttpClient(handler);
var inputContents = await client.GetStringAsync($"https://adventofcode.com/{year}/day/{day}/input");
Directory.CreateDirectory("inputs");
File.WriteAllText(Path.Combine("inputs", $"{day.ToString().PadLeft(2, '0')}.txt"), inputContents);
}
}

View File

@ -39,40 +39,88 @@ if (runPart1 != null || runPart2 != null)
if (runAll) if (runAll)
{ {
desiredDays.Clear();
foreach (var type in types) foreach (var type in types)
{ {
using var day = (Day)Activator.CreateInstance(type)!; desiredDays.Add(type.Name[^2..]);
day.Go(runPart1 ?? true, runPart2 ?? true);
} }
} }
else
{
if (desiredDays.Count == 0) if (desiredDays.Count == 0)
{ {
desiredDays.Add(""); desiredDays.Add("");
} }
var getDayNumFromArg = (string arg) =>
{
if (string.IsNullOrEmpty(arg))
{
arg = types.Last().ToString()[^2..];
}
return int.Parse(arg);
};
var getDayInstanceFromArg = (string arg) =>
{
var num = getDayNumFromArg(arg);
var typ = types.FirstOrDefault(x => x.Name == $"Day{num.ToString().PadLeft(2, '0')}");
if (typ == null)
{
return null;
}
var day = (Day?) Activator.CreateInstance(typ);
return day;
};
aoc2024.Util.DotEnv.SetEnvironment();
var sessionVal = Environment.GetEnvironmentVariable("AOC_SESSION");
if (!string.IsNullOrEmpty(sessionVal))
{
var year = Environment.GetEnvironmentVariable("AOC_YEAR");
if (string.IsNullOrEmpty(year))
{
// was going to use the current year, but this solution is specifically designed for a certain year, so using that makes more sense.
// don't forget your find/replace when copying this code for a new aoc year!
//year = DateTime.Now.Year.ToString();
year = "2024";
System.Diagnostics.Debug.WriteLine($"No AOC_YEAR env var defined or set in .env file, so assuming year {year}");
}
foreach (var day in desiredDays)
{
var dayNum = getDayNumFromArg(day);
if (!File.Exists(Path.Combine("inputs", $"{dayNum.ToString().PadLeft(2, '0')}.txt")))
{
Logger.Log($"Downloading input for day {dayNum}...");
try
{
await aoc2024.Util.RetrieveInput.Get(sessionVal, year, dayNum);
Logger.LogLine("<green>done!<r>");
}
catch (Exception ex)
{
Logger.LogLine($"<red>failed!<r> {ex}");
}
Logger.LogLine("");
}
}
}
else
{
System.Diagnostics.Debug.WriteLine("No AOC_SESSION env var defined or set in .env file, so automatic input downloading not available.");
}
foreach (var desiredDay in desiredDays) foreach (var desiredDay in desiredDays)
{ {
Day? day = null; Day? day = getDayInstanceFromArg(desiredDay);
if (string.IsNullOrEmpty(desiredDay)) if (day == null)
{
day = (Day) Activator.CreateInstance(types.Last())!;
}
else
{
var type = types.FirstOrDefault(x => x.Name == $"Day{desiredDay.PadLeft(2, '0')}");
if (type == null)
{ {
Logger.LogLine($"Unknown day <cyan>{desiredDay}<r>"); Logger.LogLine($"Unknown day <cyan>{desiredDay}<r>");
} }
else
{
day = (Day?) Activator.CreateInstance(type);
}
}
day?.Go(runPart1 ?? true, runPart2 ?? true); day?.Go(runPart1 ?? true, runPart2 ?? true);
day?.Dispose(); day?.Dispose();
} }
}