-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
75 lines (64 loc) · 2.25 KB
/
Program.cs
File metadata and controls
75 lines (64 loc) · 2.25 KB
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 AdventOfCode;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
RunMode mode = RunMode.Both;
int year = 2015;
//int day = 2;
int? day = args.Length > 0 ? int.Parse(args[0]) : (int?)null;
bool useTest = args.Contains("-t");
string fileName = useTest ? $"samples/{year}/Day{day}.txt" : $"inputs/{year}/Day{day}.txt";
var input = File.ReadAllLines(fileName);
IDaySolver solver = CreateDayInstance(year, day);
var sample = useTest ? "SAMPLE " : string.Empty; ;
if (mode == RunMode.Both || mode == RunMode.Part1)
{
string run = $"{year} Day {day} Part 1 {sample}";
Console.WriteLine($"Running {run}...");
var solution = solver.Part1(input);
Console.WriteLine($"{run} Solution: {solution}");
Console.WriteLine();
}
if (mode == RunMode.Both || mode == RunMode.Part2)
{
string run = $"{year} Day {day} Part 2 {sample}";
Console.WriteLine($"Running {run}...");
var solution = solver.Part2(input);
Console.WriteLine($"{run} Solution: {solution}");
Console.WriteLine();
}
//Console.ReadLine();
}
static IDaySolver? CreateDayInstance(int year, int? day = null)
{
string targetNamespace = $"AdventOfCode._{year}";
Type? dayType = null;
if (day.HasValue)
{
dayType = Assembly.GetExecutingAssembly()
.GetTypes()
.FirstOrDefault(t => t.Namespace == targetNamespace && t.Name == $"Day{day}" && typeof(IDaySolver).IsAssignableFrom(t));
}
else
{
dayType = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.Namespace == targetNamespace && t.Name.StartsWith($"Day") && typeof(IDaySolver).IsAssignableFrom(t))
.OrderByDescending(t => int.Parse(t.Name.Substring(3, t.Name.Length - 1)))
.FirstOrDefault();
}
if (dayType != null)
{
return (IDaySolver)Activator.CreateInstance(dayType)!;
}
return null;
}
enum RunMode
{
Part1,
Part2,
Both
}
}