From 072add7376d63a0c0b2da0a1148870e36b9d9ebd Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 30 Nov 2020 16:28:19 -0500 Subject: [PATCH] add Day base class like 2019's --- Day.cs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Day1.cs | 16 +++++++++++++++ Program.cs | 31 ++++++++++++++++++++++++++--- aoc2020.sln | 25 +++++++++++++++++++++++ 4 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 Day.cs create mode 100644 Day1.cs create mode 100644 aoc2020.sln diff --git a/Day.cs b/Day.cs new file mode 100644 index 0000000..24ffe33 --- /dev/null +++ b/Day.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; + +namespace aoc2020 +{ + public abstract class Day + { + public abstract int DayNumber { get; } + public virtual IEnumerable Input => + File.ReadLines($"input/day{DayNumber}.in"); + + public abstract string Part1(); + public abstract string Part2(); + + public virtual void AllParts(bool verbose = false) + { + Console.WriteLine($"Day {DayNumber}:"); + var s = new Stopwatch(); + + if (verbose) + { + s.Start(); + } + + var part1 = Part1(); + + if (verbose) + { + s.Stop(); + Console.WriteLine($"Part 1 elapsed ticks: {s.ElapsedTicks}"); + } + + Console.WriteLine(part1); + + if (verbose) + { + s.Reset(); + s.Start(); + } + + var part2 = Part2(); + + if (verbose) + { + s.Stop(); + Console.WriteLine($"Part 2 elapsed ticks: {s.ElapsedTicks}"); + } + + Console.WriteLine(part2); + Console.WriteLine(); + } + } +} + + diff --git a/Day1.cs b/Day1.cs new file mode 100644 index 0000000..09ae736 --- /dev/null +++ b/Day1.cs @@ -0,0 +1,16 @@ +namespace aoc2020 +{ + internal class Day1 : Day + { + public override int DayNumber => 1; + public override string Part1() + { + return ""; + } + + public override string Part2() + { + return ""; + } + } +} diff --git a/Program.cs b/Program.cs index 99fc12c..9817bf1 100644 --- a/Program.cs +++ b/Program.cs @@ -1,12 +1,37 @@ using System; +using System.Linq; +using System.Reflection; namespace aoc2020 { - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { - Console.WriteLine("Hello World!"); + var days = Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.BaseType == typeof(Day)) + .Select(t => (Day) Activator.CreateInstance(t)) + .OrderBy(d => d.DayNumber); + + if (args.Length == 1 && int.TryParse(args[0], out var dayNum)) + { + var day = days.FirstOrDefault(d => d.DayNumber == dayNum); + if (day != null) + { + day.AllParts(); + } + else + { + Console.WriteLine($"Day {dayNum} invalid or not yet implemented"); + } + } + else + { + foreach (var d in days) + { + d.AllParts(); + } + } } } } diff --git a/aoc2020.sln b/aoc2020.sln new file mode 100644 index 0000000..64abd9a --- /dev/null +++ b/aoc2020.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30709.132 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aoc2020", "aoc2020.csproj", "{3D60F1A0-0DD9-4FF9-B85B-D491B13ACAE7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3D60F1A0-0DD9-4FF9-B85B-D491B13ACAE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D60F1A0-0DD9-4FF9-B85B-D491B13ACAE7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D60F1A0-0DD9-4FF9-B85B-D491B13ACAE7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D60F1A0-0DD9-4FF9-B85B-D491B13ACAE7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F1ECEA7D-4A0E-4B78-8D3C-29D4E1D09C0D} + EndGlobalSection +EndGlobal