Runner Cleanup
This commit is contained in:
@@ -14,4 +14,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Problems\AOC2022\Day3\input.txt" />
|
||||
<None Remove="Problems\AOC2022\Day3\test.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -43,6 +43,8 @@ internal class CalorieCounting : Problem
|
||||
_mostestElf = FlaresFood
|
||||
.Select((x, idx) => (sum: x.Sum(), idx))
|
||||
.MaxBy(x => x.sum);
|
||||
|
||||
Part1 = _mostestElf.Value.ToString();
|
||||
}
|
||||
|
||||
public override void CalculatePart2()
|
||||
@@ -51,29 +53,6 @@ internal class CalorieCounting : Problem
|
||||
.Select((x, idx) => (sum: x.Sum(), idx))
|
||||
.OrderByDescending(e => e.sum)
|
||||
.Take(3);
|
||||
}
|
||||
|
||||
|
||||
public override void PrintPart1()
|
||||
{
|
||||
if (_mostestElf == null)
|
||||
{
|
||||
Console.WriteLine("Part 1 has not been calculated");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine($"Mostest: {_mostestElf}");
|
||||
}
|
||||
|
||||
public override void PrintPart2()
|
||||
{
|
||||
if(_mostestElves == null)
|
||||
{
|
||||
Console.WriteLine("Part 2 has not been calculated");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine("Top Elves");
|
||||
foreach (var elf in _mostestElves)
|
||||
Console.WriteLine($"\t{elf}");
|
||||
Console.WriteLine($"Total {_mostestElves.Sum(e => e.sum)}");
|
||||
}
|
||||
Part2 = _mostestElves.Sum(e => e.sum).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ namespace AdventOfCode.Problems.AOC2022.Day2;
|
||||
internal class RockPaperScissors : Problem
|
||||
{
|
||||
private string[] _lines;
|
||||
private int _part1Score;
|
||||
private int _part2Score;
|
||||
|
||||
public RockPaperScissors()
|
||||
{
|
||||
@@ -31,7 +29,7 @@ internal class RockPaperScissors : Problem
|
||||
totalScore += GetMoveValue(response);
|
||||
totalScore += GetResult(move, response);
|
||||
}
|
||||
_part1Score = totalScore;
|
||||
Part1 = totalScore.ToString();
|
||||
}
|
||||
|
||||
private static int GetMoveValue(char move)
|
||||
@@ -98,16 +96,6 @@ internal class RockPaperScissors : Problem
|
||||
score += GetResponseValue(move, result);
|
||||
score += GetResultValue(result);
|
||||
}
|
||||
_part2Score = score;
|
||||
}
|
||||
|
||||
public override void PrintPart1()
|
||||
{
|
||||
Console.WriteLine($"P1: {_part1Score}");
|
||||
}
|
||||
|
||||
public override void PrintPart2()
|
||||
{
|
||||
Console.WriteLine($"P2: {_part2Score}");
|
||||
Part2 = score.ToString();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
using AdventOfCode.Problems.AOC2022.Day1;
|
||||
using AdventOfCode.Problems.AOC2022.Day2;
|
||||
using AdventOfCode.Runner;
|
||||
using AdventOfCode.Runner;
|
||||
|
||||
namespace AdventOfCode;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var runner = new AOCRunner();
|
||||
runner.RenderMenu();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,16 +36,38 @@ public class AOCRunner
|
||||
|
||||
public void RenderMenu()
|
||||
{
|
||||
var years = _loadedProblems.Keys.OrderByDescending(k => k);
|
||||
|
||||
var defaultYear = DateTime.Now.Year.ToString();
|
||||
|
||||
Console.WriteLine($"Select a Year: (blank is {defaultYear})");
|
||||
RenderYears(defaultYear);
|
||||
|
||||
Console.Write("Select a Year: ");
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"(blank is {defaultYear})");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
var inputYear = Console.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(inputYear))
|
||||
inputYear = defaultYear;
|
||||
|
||||
RenderYearMenu(defaultYear);
|
||||
RenderYearMenu(inputYear);
|
||||
}
|
||||
|
||||
private void RenderYears(string defaultYear)
|
||||
{
|
||||
var years = _loadedProblems.Keys.OrderByDescending(k => k);
|
||||
|
||||
foreach (var year in years)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
if (defaultYear == year)
|
||||
Console.Write("\t> ");
|
||||
else
|
||||
Console.Write("\t ");
|
||||
|
||||
Console.Write($"{year} ");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Console.WriteLine($"- {_loadedProblems[year].Count} Problems");
|
||||
}
|
||||
}
|
||||
|
||||
private void RenderYearMenu(string year)
|
||||
@@ -55,6 +77,7 @@ public class AOCRunner
|
||||
Console.WriteLine($"No problems for {year} exist");
|
||||
return;
|
||||
}
|
||||
var defaultDay = DateTime.Now.Day;
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"{year}:");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
@@ -63,15 +86,21 @@ public class AOCRunner
|
||||
{
|
||||
var (info, _) = days[i];
|
||||
Console.ForegroundColor = ConsoleColor.Magenta;
|
||||
Console.Write($"\t [{i}]");
|
||||
if(i == defaultDay)
|
||||
Console.Write($"\t> [{i}]");
|
||||
else
|
||||
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})");
|
||||
Console.Write($"Select Day Index: ");
|
||||
Console.ForegroundColor = ConsoleColor.Magenta;
|
||||
Console.WriteLine($"(blank is {defaultDay})");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
|
||||
var inputDay = Console.ReadLine();
|
||||
|
||||
if(!int.TryParse(inputDay, out var parsedDay))
|
||||
@@ -88,20 +117,22 @@ public class AOCRunner
|
||||
|
||||
Console.Clear();
|
||||
var (info, problemType) = yearList[dayIndex];
|
||||
Console.Write("Problem: ");
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine($"\t{info.Name}");
|
||||
Console.ForegroundColor = ConsoleColor.DarkRed;
|
||||
Console.WriteLine($"\t\t{info.Year} - Day {info.Day}");
|
||||
|
||||
Console.WriteLine($"Problem: \t{info.Name}");
|
||||
Console.WriteLine($"\t\t{info.Year} - {info.Day}");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
|
||||
|
||||
var problem = Activator.CreateInstance(problemType) as Problem;
|
||||
if(problem == null ) {
|
||||
if (Activator.CreateInstance(problemType) is not Problem problem)
|
||||
{
|
||||
Console.WriteLine("Failed to create problem isntance");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine("Loading Input data...");
|
||||
problem.LoadInput();
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Loading Input data...\n");
|
||||
problem.LoadInput();
|
||||
|
||||
RunPart("Calculating Part 1", problem.CalculatePart1);
|
||||
RunPart("Calculating Part 2", problem.CalculatePart2);
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
using AdventOfCode.Runner.Attributes;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AdventOfCode.Runner;
|
||||
public abstract class Problem
|
||||
{
|
||||
public abstract void LoadInput();
|
||||
public abstract void CalculatePart1();
|
||||
public abstract void PrintPart1();
|
||||
public abstract void CalculatePart2();
|
||||
public abstract void PrintPart2();
|
||||
|
||||
protected string GetInputFile(string filename)
|
||||
{
|
||||
var info = this.GetType().GetCustomAttribute<ProblemInfoAttribute>();
|
||||
if (info == null)
|
||||
return filename;
|
||||
|
||||
return Path.Combine($"Problems/AOC{info.Year}/Day{info.Day}", filename);
|
||||
}
|
||||
}
|
||||
71
AdventOfCode/Runner/Problem.cs
Normal file
71
AdventOfCode/Runner/Problem.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using AdventOfCode.Runner.Attributes;
|
||||
|
||||
using System.Reflection;
|
||||
|
||||
namespace AdventOfCode.Runner;
|
||||
|
||||
public abstract class Problem
|
||||
{
|
||||
protected string? Part1 { get; set; }
|
||||
protected string? Part2 { get; set; }
|
||||
|
||||
public abstract void LoadInput();
|
||||
|
||||
public abstract void CalculatePart1();
|
||||
|
||||
public virtual void PrintPart1() {
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
if (Part1 == null)
|
||||
{
|
||||
Console.Write("Part 1: ");
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("No Solution");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write("Part 1: ");
|
||||
Console.ForegroundColor = ConsoleColor.Blue;
|
||||
Console.WriteLine($"{Part1}");
|
||||
}
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
}
|
||||
|
||||
public abstract void CalculatePart2();
|
||||
|
||||
public virtual void PrintPart2()
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
if (Part2 == null)
|
||||
{
|
||||
Console.Write("Part 2: ");
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("No Solution");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write("Part 2: ");
|
||||
Console.ForegroundColor = ConsoleColor.Blue;
|
||||
Console.WriteLine($"{Part2}");
|
||||
}
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
}
|
||||
|
||||
protected string GetInputFile(string filename = "input.txt")
|
||||
{
|
||||
var info = this.GetType().GetCustomAttribute<ProblemInfoAttribute>();
|
||||
if (info == null)
|
||||
return filename;
|
||||
|
||||
return Path.Combine($"Problems/AOC{info.Year}/Day{info.Day}", filename);
|
||||
}
|
||||
|
||||
protected string[] ReadInputLines(string filename = "input.txt")
|
||||
{
|
||||
return File.ReadAllLines(GetInputFile(filename));
|
||||
}
|
||||
|
||||
protected string ReadInputText(string filename = "input.txt")
|
||||
{
|
||||
return File.ReadAllText(GetInputFile(filename));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user