ben
/
aoc
1
0
Fork 0
aoc/AOC.Common/Day.cs

127 lines
4.0 KiB
C#
Raw Normal View History

2023-11-25 18:21:39 +00:00
using CommandLine;
2022-12-03 05:55:49 +00:00
namespace AOC.Common;
2023-09-20 15:41:01 +00:00
public abstract class Day(int year, int day, string puzzleName)
{
2023-09-20 15:41:01 +00:00
public int Year { get; } = year;
public int DayNumber { get; } = day;
public string PuzzleName { get; } = puzzleName;
2023-11-25 23:36:17 +00:00
protected IEnumerable<string> Input => File.ReadLines(FileName);
public string FileName =>
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
$"input{Year}/{(UseTestInput ? "test" : "day")}{DayNumber,2:00}.in");
2023-11-25 23:36:17 +00:00
public static bool UseTestInput { get; set; }
private readonly Stopwatch _stopwatch = new();
2023-12-01 07:30:47 +00:00
public abstract void ProcessInput();
public abstract object Part1();
public abstract object Part2();
2023-11-25 18:21:39 +00:00
private void PrintProcessInput()
{
2023-11-25 18:21:39 +00:00
_stopwatch.Restart();
ProcessInput();
2023-11-25 18:21:39 +00:00
_stopwatch.Stop();
Console.WriteLine(
2023-11-26 03:54:09 +00:00
$"{Year} Day {DayNumber,2}: {PuzzleName,-40} {_stopwatch.ScaleMilliseconds()}ms elapsed processing input");
2023-11-25 18:21:39 +00:00
}
2022-12-09 16:59:43 +00:00
2023-11-25 18:21:39 +00:00
private void PrintPart1()
{
_stopwatch.Restart();
var part1 = Part1();
2023-11-25 18:21:39 +00:00
_stopwatch.Stop();
2023-11-16 18:50:27 +00:00
2023-11-26 03:54:09 +00:00
Console.WriteLine($"Part 1: {part1,-45} {_stopwatch.ScaleMilliseconds()}ms elapsed");
2023-11-25 18:21:39 +00:00
}
2023-11-25 18:21:39 +00:00
private void PrintPart2()
{
_stopwatch.Restart();
var part2 = Part2();
2023-11-25 18:21:39 +00:00
_stopwatch.Stop();
2023-11-26 03:54:09 +00:00
Console.WriteLine($"Part 2: {part2,-45} {_stopwatch.ScaleMilliseconds()}ms elapsed");
}
2023-11-25 23:36:17 +00:00
private class Options
{
[Option('t', "test", Required = false, Default = false, HelpText = "Use test input for the given day")]
public bool TestInput { get; set; }
[Option('a', "all", Required = false, Default = false, HelpText = "Run all available days. Overrides day and part.")]
public bool RunAllDays { get; set; }
[Value(0, MetaName = "Day Number", HelpText = "Which Day to run")]
public int DayNumber { get; set; }
[Value(1, MetaName = "Part", HelpText = "Which Part to run")]
public int? PartNumber { get; set; }
}
public static void RunFromArgs(string[] args)
{
var days = Assembly.GetEntryAssembly()?.GetTypes()
.Where(t => t.BaseType == typeof(Day))
.Select(t => (Activator.CreateInstance(t) as Day)!)
.OrderBy(d => d.DayNumber)
.ToList();
2023-09-20 19:58:23 +00:00
if (days == null || days.Count == 0)
throw new ApplicationException("no days found");
2023-09-20 19:58:23 +00:00
2023-11-25 18:21:39 +00:00
Parser.Default.ParseArguments<Options>(args).WithParsed(options =>
{
2023-11-25 18:21:39 +00:00
UseTestInput = options.TestInput;
2023-11-25 23:36:17 +00:00
if (options.RunAllDays)
2023-11-25 18:21:39 +00:00
{
2023-11-25 23:36:17 +00:00
foreach (var day in days)
2023-11-25 18:21:39 +00:00
{
2023-11-25 23:36:17 +00:00
day.PrintProcessInput();
2023-11-25 18:21:39 +00:00
day.PrintPart1();
day.PrintPart2();
2023-11-25 23:36:17 +00:00
Console.WriteLine();
2023-11-25 18:21:39 +00:00
}
2023-11-25 23:36:17 +00:00
}
else
{
var day = days.SingleOrDefault(d => d.DayNumber == options.DayNumber);
if (day != null)
{
day.PrintProcessInput();
if (options.PartNumber.HasValue)
{
switch (options.PartNumber)
{
case 1:
day.PrintPart1();
break;
case 2:
day.PrintPart2();
break;
default:
throw new ArgumentOutOfRangeException(nameof(args));
}
}
else
{
day.PrintPart1();
day.PrintPart2();
}
2023-11-25 18:21:39 +00:00
2023-11-25 23:36:17 +00:00
Console.WriteLine();
}
else throw new ApplicationException($"Day {options.DayNumber} invalid or not yet implemented");
2023-11-25 18:21:39 +00:00
}
}).WithNotParsed(errors =>
{
2023-11-25 18:21:39 +00:00
foreach (var err in errors) Console.WriteLine(err);
});
}
2022-12-09 16:59:43 +00:00
}