Updates to problem runner

This commit is contained in:
2022-12-02 22:10:51 -05:00
parent cf38986426
commit 3b13aa8585
7 changed files with 135 additions and 53 deletions

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@@ -7,22 +7,11 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="Problems\AOC2022\Day1\input.txt" />
<None Remove="Problems\AOC2022\Day2\input.txt" />
<None Remove="Problems\AOC2022\Day2\test.txt" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Problems\AOC2022\Day1\input.txt"> <EmbeddedResource Include="Problems\*\*\*.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Include="Problems\AOC2022\Day2\input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Problems\AOC2022\Day2\test.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -3,30 +3,30 @@ using AdventOfCode.Runner.Attributes;
namespace AdventOfCode.Problems.AOC2022.Day0; namespace AdventOfCode.Problems.AOC2022.Day0;
[ProblemInfo("2022", 0, "Fancy Test")] [ProblemInfo("2022", 0, "Fancy Test")]
public class TestProblem : IProblem public class TestProblem : Problem
{ {
public void LoadInput() public override void LoadInput()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} }
public void CalculatePart1() public override void CalculatePart1()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} }
public void CalculatePart2() public override void CalculatePart2()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} }
public void PrintPart1() public override void PrintPart1()
{ {
Console.WriteLine("Result"); Console.WriteLine("Result");
} }
public void PrintPart2() public override void PrintPart2()
{ {
Console.WriteLine("Result 2"); Console.WriteLine("Result 2");
} }

View File

@@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day1; namespace AdventOfCode.Problems.AOC2022.Day1;
[ProblemInfo("2022", 1, "Calorie Counting")] [ProblemInfo("2022", 1, "Calorie Counting")]
internal class CalorieCounting : IProblem internal class CalorieCounting : Problem
{ {
public List<List<int>> FlaresFood { get; set; } public List<List<int>> FlaresFood { get; set; }
@@ -23,9 +23,9 @@ internal class CalorieCounting : IProblem
}; };
} }
public void LoadInput() public override void LoadInput()
{ {
var lines = File.ReadAllLines("Problems/AOC2022/Day1/input.txt"); var lines = File.ReadAllLines(GetInputFile("input.txt"));
var c = 0; var c = 0;
foreach (var calorie in lines) foreach (var calorie in lines)
{ {
@@ -38,14 +38,14 @@ internal class CalorieCounting : IProblem
FlaresFood[c].Add(int.Parse(calorie)); FlaresFood[c].Add(int.Parse(calorie));
} }
} }
public void CalculatePart1() public override void CalculatePart1()
{ {
_mostestElf = FlaresFood _mostestElf = FlaresFood
.Select((x, idx) => (sum: x.Sum(), idx)) .Select((x, idx) => (sum: x.Sum(), idx))
.MaxBy(x => x.sum); .MaxBy(x => x.sum);
} }
public void CalculatePart2() public override void CalculatePart2()
{ {
_mostestElves = FlaresFood _mostestElves = FlaresFood
.Select((x, idx) => (sum: x.Sum(), idx)) .Select((x, idx) => (sum: x.Sum(), idx))
@@ -54,7 +54,7 @@ internal class CalorieCounting : IProblem
} }
public void PrintPart1() public override void PrintPart1()
{ {
if (_mostestElf == null) if (_mostestElf == null)
{ {
@@ -64,7 +64,7 @@ internal class CalorieCounting : IProblem
Console.WriteLine($"Mostest: {_mostestElf}"); Console.WriteLine($"Mostest: {_mostestElf}");
} }
public void PrintPart2() public override void PrintPart2()
{ {
if(_mostestElves == null) if(_mostestElves == null)
{ {

View File

@@ -3,30 +3,30 @@ using AdventOfCode.Runner.Attributes;
namespace AdventOfCode.Problems.AOC2023.Day0; namespace AdventOfCode.Problems.AOC2023.Day0;
[ProblemInfo("2023", 0, "Test")] [ProblemInfo("2023", 0, "Test")]
public class TestProblem : IProblem public class TestProblem : Problem
{ {
public void LoadInput() public override void LoadInput()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} }
public void CalculatePart1() public override void CalculatePart1()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} }
public void CalculatePart2() public override void CalculatePart2()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} }
public void PrintPart1() public override void PrintPart1()
{ {
Console.WriteLine("Result"); Console.WriteLine("Result");
} }
public void PrintPart2() public override void PrintPart2()
{ {
Console.WriteLine("Result 2"); Console.WriteLine("Result 2");
} }

View File

@@ -10,11 +10,5 @@ internal class Program
{ {
var runner = new AOCRunner(); var runner = new AOCRunner();
runner.RenderMenu(); runner.RenderMenu();
var cc = new RockPaperScissors();
cc.LoadInput();
cc.CalculatePart1();
cc.PrintPart1();
cc.CalculatePart2();
cc.PrintPart2();
} }
} }

View File

@@ -1,5 +1,6 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System.Diagnostics;
using System.Reflection; using System.Reflection;
namespace AdventOfCode.Runner; namespace AdventOfCode.Runner;
@@ -16,7 +17,7 @@ public class AOCRunner
private void FindProblemClasses() private void FindProblemClasses()
{ {
var types = Assembly.GetExecutingAssembly().DefinedTypes.Where(t => t.IsAssignableTo(typeof(IProblem)) && !t.IsInterface); var types = Assembly.GetExecutingAssembly().DefinedTypes.Where(t => t.IsAssignableTo(typeof(Problem)) && !t.IsInterface);
if (types == null) if (types == null)
return; return;
foreach (var type in types) foreach (var type in types)
@@ -37,21 +38,110 @@ public class AOCRunner
{ {
var years = _loadedProblems.Keys.OrderByDescending(k => k); var years = _loadedProblems.Keys.OrderByDescending(k => k);
Console.WriteLine("Available Problems:"); var defaultYear = DateTime.Now.Year.ToString();
foreach (var year in years)
RenderYearMenu(year); Console.WriteLine($"Select a Year: (blank is {defaultYear})");
var inputYear = Console.ReadLine();
if (string.IsNullOrWhiteSpace(inputYear))
inputYear = defaultYear;
RenderYearMenu(defaultYear);
} }
private void RenderYearMenu(string year) private void RenderYearMenu(string year)
{ {
if(!_loadedProblems.ContainsKey(year))
{
Console.WriteLine($"No problems for {year} exist");
return;
}
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{year}:"); Console.WriteLine($"{year}:");
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
var days = _loadedProblems[year]; var days = _loadedProblems[year];
for (int i = 0; i < days.Count; i++) for (int i = 0; i < days.Count; i++)
{ {
var (info, type) = days[i]; var (info, _) = days[i];
Console.WriteLine($"\tDay {info.Day} - {info.Name}"); Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write($"\t [{i}]");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($" - Day {info.Day} - {info.Name}");
}
Console.WriteLine();
var defaultDay = DateTime.Now.Day;
Console.WriteLine($"Select Day Index: (blank is {defaultDay})");
var inputDay = Console.ReadLine();
if(!int.TryParse(inputDay, out var parsedDay))
parsedDay = defaultDay;
RunDay(year, parsedDay);
}
private void RunDay(string year, int dayIndex)
{
var yearList = _loadedProblems[year];
if (yearList.Count <= dayIndex || dayIndex < 0)
Console.WriteLine($"No problem exists for day index {dayIndex}");
Console.Clear();
var (info, problemType) = yearList[dayIndex];
Console.WriteLine($"Problem: \t{info.Name}");
Console.WriteLine($"\t\t{info.Year} - {info.Day}");
var problem = Activator.CreateInstance(problemType) as Problem;
if(problem == null ) {
Console.WriteLine("Failed to create problem isntance");
return;
}
Console.WriteLine("Loading Input data...");
problem.LoadInput();
Console.WriteLine();
RunPart("Calculating Part 1", problem.CalculatePart1);
RunPart("Calculating Part 2", problem.CalculatePart2);
Console.WriteLine();
Console.WriteLine("Printing Results:");
Console.WriteLine("---- Part 1 ----");
problem.PrintPart1();
Console.Write("\n\n");
Console.WriteLine("---- Part 2 ----");
problem.PrintPart2();
Console.Write("\n\n");
}
private static void RunPart(string name, Action action)
{
Console.ForegroundColor = ConsoleColor.Gray;
var sw = new Stopwatch();
Console.Write($"{name}... ");
try
{
sw.Start();
action();
sw.Stop();
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Done in ");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"{sw.ElapsedMilliseconds}ms");
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed");
Console.WriteLine(e);
}
finally
{
sw.Stop();
Console.ForegroundColor = ConsoleColor.Gray;
} }
} }
} }

View File

@@ -1,18 +1,27 @@
using System; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AdventOfCode.Runner; namespace AdventOfCode.Runner;
public interface IProblem public abstract class Problem
{ {
void LoadInput(); public abstract void LoadInput();
public abstract void CalculatePart1();
public abstract void PrintPart1();
public abstract void CalculatePart2();
public abstract void PrintPart2();
void CalculatePart1(); protected string GetInputFile(string filename)
{
void PrintPart1(); var info = this.GetType().GetCustomAttribute<ProblemInfoAttribute>();
if (info == null)
void CalculatePart2(); return filename;
void PrintPart2();
return Path.Combine($"Problems/AOC{info.Year}/Day{info.Day}", filename);
}
} }