-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cs
131 lines (114 loc) · 4.05 KB
/
Application.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Reflection;
namespace adventofcode;
public class Application
{
public void Run(string[] args)
{
Console.WriteLine("Hello to adventofcode!");
// Check if arguments were passed
if (args.Length > 0)
{
Console.WriteLine("Command-line arguments received:");
foreach (var arg in args)
{
Console.WriteLine(arg);
}
}
else
{
Console.WriteLine("No command-line arguments received.");
}
PreparedArgs preparedArgs = CheckArgs(args);
if(!preparedArgs.IsValid)
{
Console.WriteLine("Invalid arguments. Exiting...");
return;
}
Solve(preparedArgs);
}
private PreparedArgs CheckArgs(string[] args)
{
int year = 0;
int day = 0;
int part = 1;
bool bothParts = false;
try
{
if(args.Length > 3)
{
Console.WriteLine("Too many arguments. Please provide year, day, and part.");
return new PreparedArgs { IsValid = false };
}
else if(args.Length == 3)
{
year = Convert.ToInt32(args[0]);
day = Convert.ToInt32(args[1]);
part = Convert.ToInt32(args[2]);
}
else if (args.Length == 2)
{
year = Convert.ToInt32(args[0]);
day = Convert.ToInt32(args[1]);
bothParts = true;
}
else
{
var today = new DateTime();
today = DateTime.Now;
var start = new DateTime(2023, 12, 01);
if(today.Month != 12 || today.Day > 25)
{
today = start;
Console.WriteLine("It's not December or it's after the 25th. Setting today to December 1st, 2023.");
}
year = today.Year;
day = today.Day;
bothParts = true;
}
Console.WriteLine("You selected year {0} day {1} part {2} to solve. Let's go and try it...", year.ToString(), day.ToString(), part.ToString());
return new PreparedArgs { IsValid = true, Year = year, Day = day, Part = part, BothParts = bothParts };
}
catch (Exception)
{
Console.WriteLine("Invalid format. Please ensure all arguments are positive integers.");
return new PreparedArgs { IsValid = false };
}
}
private void Solve(PreparedArgs preparedArgs)
{
try
{
if (preparedArgs.Year == 2024 || preparedArgs.Year == 2019)
{
Assembly assembly = Assembly.GetExecutingAssembly();
var solution = $"adventofcode.Year{preparedArgs.Year}.Day{preparedArgs.Day.ToString("D2")}.Solution";
Console.WriteLine(solution);
Type type = assembly.GetType(solution);
if (type == null)
{
Console.WriteLine("The solution could not be found. Please try again.");
return;
}
object instance = Activator.CreateInstance(type);
if (preparedArgs.BothParts)
{
MethodInfo solvePart1 = type.GetMethod("SolvePart1");
solvePart1.Invoke(instance, null);
MethodInfo solvePart2 = type.GetMethod("SolvePart2");
solvePart2.Invoke(instance, null);
}
else
{
MethodInfo solve = type.GetMethod($"SolvePart{preparedArgs.Part}");
solve.Invoke(instance, null);
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while trying to solve the problem. Please try again.");
Console.WriteLine(ex.Message);
}
}
}