Added a Downloader for AoC puzzle inputs

This commit is contained in:
2025-12-14 16:06:38 +01:00
parent e68be83627
commit 0b0f4eabb4
5 changed files with 53 additions and 3 deletions
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+34
View File
@@ -0,0 +1,34 @@
// See https://aka.ms/new-console-template for more information
if (args.Length < 3)
{
Console.WriteLine("Usage: AdventOfCode-Downloader.exe <year> <day> <session-cookie>");
return;
}
int year, day;
if (!(int.TryParse(args[0], out year) && year <= DateTime.Now.Year && year > 2015) || !(int.TryParse(args[1], out day) && day is >= 1 and <= 25))
{
Console.WriteLine("Invalid year or date argument.\nYear must be between 2015 and current year and day must be between 1 and 25");
return;
}
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "AoC-PuzzleInputs",
year.ToString(), "Prod", $"day{day:00}.txt");
if(!Directory.Exists(Path.GetDirectoryName(path)))
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
var url = new Uri($"https://adventofcode.com/{year}/day/{day}/input");
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Cookie", $"session={args[2]}");
var responseMessage = await client.GetAsync(url);
if (responseMessage.IsSuccessStatusCode)
{
var inputString = await responseMessage.Content.ReadAsStringAsync();
await File.WriteAllTextAsync(path, inputString);
}
else
{
var inputString = await responseMessage.Content.ReadAsStringAsync();
Console.WriteLine($"Response status code does not indicate Success: {responseMessage.StatusCode} ({responseMessage.StatusCode.ToString()})\nError Message: " + inputString);
}