59 lines
1.5 KiB
C#
59 lines
1.5 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using AdventOfCode;
|
|
using AdventOfCode.Days;
|
|
using BenchmarkDotNet.Running;
|
|
using Spectre.Console;
|
|
|
|
// Benchmark
|
|
if (args is ["--bench" or "-b"])
|
|
{
|
|
BenchmarkRunner.Run<DayBenchmark>();
|
|
Environment.Exit(0);
|
|
}
|
|
|
|
// Normal run
|
|
var days = Assembly.GetAssembly(typeof(Day))!.GetTypes()
|
|
.Where(t => t.IsAssignableTo(typeof(Day)) && t.GetConstructor(Type.EmptyTypes) != null && !t.IsAbstract)
|
|
.Select(t => (Day)Activator.CreateInstance(t)!);
|
|
|
|
var select = new SelectionPrompt<Day>()
|
|
.Title("[cyan]Select a [yellow]day[/] to run:[/]")
|
|
.AddChoices(days.OrderByDescending(d => d.Number));
|
|
|
|
var selectedDay = AnsiConsole.Prompt(select);
|
|
|
|
var stopWatch = new Stopwatch();
|
|
|
|
AnsiConsole.MarkupLine($"[cyan]Running [yellow]{selectedDay}[/]...[/]\n");
|
|
|
|
AnsiConsole.MarkupLine("[cyan]Part [yellow]1[/] result:[/]");
|
|
|
|
try
|
|
{
|
|
stopWatch.Start();
|
|
selectedDay.RunPart1();
|
|
stopWatch.Stop();
|
|
|
|
AnsiConsole.MarkupLine($"[red]Approximate run time: [yellow]{stopWatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000:F3} ms[/][/]");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
AnsiConsole.WriteException(e);
|
|
}
|
|
|
|
stopWatch.Reset();
|
|
|
|
AnsiConsole.MarkupLine("\n[cyan]Part [yellow]2[/] result:[/]");
|
|
try
|
|
{
|
|
stopWatch.Start();
|
|
selectedDay.RunPart2();
|
|
stopWatch.Stop();
|
|
|
|
AnsiConsole.MarkupLine($"[red]Approximate run time: [yellow]{stopWatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000:F3} ms[/][/]");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
AnsiConsole.WriteException(e);
|
|
} |