add DayFactory and cli options for day
This commit is contained in:
parent
c8fd30d2ec
commit
a67b0bfdbe
|
@ -0,0 +1,13 @@
|
|||
namespace aoc2019
|
||||
{
|
||||
public abstract class Day
|
||||
{
|
||||
public virtual void AllParts()
|
||||
{
|
||||
Part1();
|
||||
Part2();
|
||||
}
|
||||
public abstract void Part1();
|
||||
public abstract void Part2();
|
||||
}
|
||||
}
|
10
Day1.cs
10
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());
|
||||
}
|
||||
|
|
8
Day2.cs
8
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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
30
Program.cs
30
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue