-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
75 lines (65 loc) · 2.48 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.CommandLine;
using System.IO;
using adventofcode.Core;
namespace adventofcode
{
class Program
{
static int Main(string[] args)
{
var rootCommand = new RootCommand("Outputs answers for Advent of Code challenges.");
var yearOption = new Option<int>(
new string[] { "-year", "-y"},
"Will output results for all Solutions for the supplied Year.") { IsRequired = true } ;
var dayOption = new Option<int>(
new string[] { "-day", "-d"},
"Will output results for all Solutions for the supplied Day.") { IsRequired = true } ;
var dayYearCommand = new Command("solve"){
dayOption,
yearOption
};
dayYearCommand.SetHandler((day, year) =>
{
SolveDay(year, day);
}, dayOption, yearOption);
rootCommand.AddCommand(dayYearCommand);
var allCommand = new Command("all");
allCommand.SetHandler(() => SolveAll());
rootCommand.AddCommand(allCommand);
Directory.SetCurrentDirectory(Path.Combine(AppContext.BaseDirectory, "..", "..", ".."));
return rootCommand.InvokeAsync(args).Result;
}
private static void SolveAll()
{
var solutions = SolutionFinder.FindSolutions();
foreach (var solution in solutions)
{
var attributeData = solution.GetCustomAttributesData();
var customAttributes = solution.GetCustomAttributes(false);
foreach (var attribute in customAttributes)
{
if (attribute is SolutionAttribute solutionAttribute)
{
Console.WriteLine($"Answers for {solutionAttribute.ProblemName}");
}
}
var instance = (Solution)Activator.CreateInstance(solution);
instance.Solve();
}
}
private static void SolveDay(int year, int day)
{
var solutions = SolutionFinder.FindSolutions();
foreach (var solution in solutions)
{
var instance = (Solution)Activator.CreateInstance(solution);
if (instance.Year != year || instance.Day != day)
{
continue;
}
instance.Solve();
}
}
}
}