diff options
-rw-r--r-- | Day.cs | 13 | ||||
-rw-r--r-- | Day1.cs | 10 | ||||
-rw-r--r-- | Day2.cs | 8 | ||||
-rw-r--r-- | DayFactory.cs | 17 | ||||
-rw-r--r-- | Program.cs | 30 | ||||
-rw-r--r-- | aoc2019.csproj | 9 |
6 files changed, 71 insertions, 16 deletions
diff --git a/Day.cs b/Day.cs new file mode 100644 index 0000000..c02c37b --- /dev/null +++ b/Day.cs @@ -0,0 +1,13 @@ +namespace aoc2019 +{ + public abstract class Day + { + public virtual void AllParts() + { + Part1(); + Part2(); + } + public abstract void Part1(); + public abstract void Part2(); + } +} diff --git a/Day1.cs b/Day1.cs index 570d017..186c7b4 100644 --- a/Day1.cs +++ b/Day1.cs @@ -5,21 +5,21 @@ using System.Linq; namespace aoc2019 { - public class Day1 + public class Day1 : Day { - private static IEnumerable<int> lines = + private static readonly IEnumerable<int> lines = File.ReadLines("input/day1.in").Select(line => int.Parse(line)); private static int FuelCost(int weight) => weight / 3 - 2; - public static void Part1() + public override void Part1() { Console.WriteLine(lines.Select(num => FuelCost(num)).Sum()); } private static int FullCost(int cost) { - int total = 0, newcost = 0, tmp = cost; + int total = 0, newcost, tmp = cost; while ((newcost = FuelCost(tmp)) >= 0) { @@ -30,7 +30,7 @@ namespace aoc2019 return total; } - public static void Part2() + public override void Part2() { Console.WriteLine(lines.Select(cost => FullCost(cost)).Sum()); } diff --git a/Day2.cs b/Day2.cs index 3cbb1ec..10dc15f 100644 --- a/Day2.cs +++ b/Day2.cs @@ -5,9 +5,9 @@ using System.Linq; namespace aoc2019 { - public class Day2 + public class Day2 : Day { - private static IEnumerable<int> input = + private static readonly IEnumerable<int> input = File.ReadLines("input/day2.in") .First() .Split(',') @@ -23,7 +23,7 @@ namespace aoc2019 } } - public static void Part1() + public override void Part1() { var output = input.ToList(); output[1] = 12; @@ -34,7 +34,7 @@ namespace aoc2019 Console.WriteLine($"{output[0]}"); } - public static void Part2() + public override void Part2() { List<int> output; diff --git a/DayFactory.cs b/DayFactory.cs new file mode 100644 index 0000000..6574a8d --- /dev/null +++ b/DayFactory.cs @@ -0,0 +1,17 @@ +using System; + +namespace aoc2019 +{ + internal class DayFactory + { + internal static Day GetDay(int daynum) + { + switch (daynum) + { + case 1: return new Day1(); + case 2: return new Day2(); + default: return null; + } + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 62d0cb1..146196e 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,4 @@ using System; -using System.IO; -using System.Linq; namespace aoc2019 { @@ -8,11 +6,29 @@ namespace aoc2019 { static void Main(string[] args) { - Day1.Part1(); - Day1.Part2(); - - Day2.Part1(); - Day2.Part2(); + if (args.Length == 1 && int.TryParse(args[0], out int daynum)) + { + if (daynum >= 0 && daynum <= 25) + { + Day day = DayFactory.GetDay(daynum); + day.AllParts(); + } + else + { + Console.WriteLine($"{daynum} is an invalid day"); + return; + } + } + else + { + for (var i = 1; i <= 25; ++i) + { + var day = DayFactory.GetDay(i); + if (day == null) continue; + Console.WriteLine($"Day {i}:"); + day.AllParts(); + } + } } } } diff --git a/aoc2019.csproj b/aoc2019.csproj index 7322fb3..83016c1 100644 --- a/aoc2019.csproj +++ b/aoc2019.csproj @@ -5,4 +5,13 @@ <TargetFramework>netcoreapp3.0</TargetFramework> </PropertyGroup> + <ItemGroup> + <None Update="input\day1.in"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </None> + <None Update="input\day2.in"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </None> + </ItemGroup> + </Project> |