Files
AdventOfCode/Program.cs
2024-06-03 17:29:56 +02:00

74 lines
1.6 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();
// Read input
try
{
selectedDay.ReadInput();
}
catch (Exception e)
{
AnsiConsole.WriteException(e);
Environment.Exit(1);
}
// Part 1
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();
// Part 2
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);
}