-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
App.cs
181 lines (155 loc) · 6.23 KB
/
App.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using AdventOfCode;
var tsolvers = Assembly.GetEntryAssembly()!.GetTypes()
.Where(t => t.GetTypeInfo().IsClass && typeof(Solver).IsAssignableFrom(t))
.OrderBy(t => t.FullName)
.ToArray();
var action =
Command(args, Args("update", "([0-9]+)/([0-9]+)"), m => {
var year = int.Parse(m[1]);
var day = int.Parse(m[2]);
return () => new Updater().Update(year, day).Wait();
}) ??
Command(args, Args("update", "today"), m => {
var dt = DateTime.UtcNow.AddHours(-5);
if (dt is { Month: 12, Day: >= 1 and <= 25 }) {
return () => new Updater().Update(dt.Year, dt.Day).Wait();
} else {
throw new AocCommuncationError("Event is not active. This option works in Dec 1-25 only)");
}
}) ??
Command(args, Args("upload", "([0-9]+)/([0-9]+)"), m => {
var year = int.Parse(m[1]);
var day = int.Parse(m[2]);
return () => {
var tsolver = tsolvers.First(tsolver =>
SolverExtensions.Year(tsolver) == year &&
SolverExtensions.Day(tsolver) == day);
new Updater().Upload(GetSolvers(tsolver)[0]).Wait();
};
}) ??
Command(args, Args("upload", "today"), m => {
var dt = DateTime.UtcNow.AddHours(-5);
if (dt is { Month: 12, Day: >= 1 and <= 25 }) {
var tsolver = tsolvers.First(tsolver =>
SolverExtensions.Year(tsolver) == dt.Year &&
SolverExtensions.Day(tsolver) == dt.Day);
return () =>
new Updater().Upload(GetSolvers(tsolver)[0]).Wait();
} else {
throw new AocCommuncationError("Event is not active. This option works in Dec 1-25 only)");
}
}) ??
Command(args, Args("([0-9]+)/(Day)?([0-9]+)"), m => {
var year = int.Parse(m[0]);
var day = int.Parse(m[2]);
var tsolversSelected = tsolvers.First(tsolver =>
SolverExtensions.Year(tsolver) == year &&
SolverExtensions.Day(tsolver) == day);
return () => Runner.RunAll(GetSolvers(tsolversSelected));
}) ??
Command(args, Args("[0-9]+"), m => {
var year = int.Parse(m[0]);
var tsolversSelected = tsolvers.Where(tsolver =>
SolverExtensions.Year(tsolver) == year);
return () => Runner.RunAll(GetSolvers(tsolversSelected.ToArray()));
}) ??
Command(args, Args("([0-9]+)/all"), m => {
var year = int.Parse(m[0]);
var tsolversSelected = tsolvers.Where(tsolver =>
SolverExtensions.Year(tsolver) == year);
return () => Runner.RunAll(GetSolvers(tsolversSelected.ToArray()));
}) ??
Command(args, Args("all"), m => {
return () => Runner.RunAll(GetSolvers(tsolvers));
}) ??
Command(args, Args("today"), m => {
var dt = DateTime.UtcNow.AddHours(-5);
if (dt is { Month: 12, Day: >= 1 and <= 25 }) {
var tsolversSelected = tsolvers.First(tsolver =>
SolverExtensions.Year(tsolver) == dt.Year &&
SolverExtensions.Day(tsolver) == dt.Day);
return () =>
Runner.RunAll(GetSolvers(tsolversSelected));
} else {
throw new AocCommuncationError("Event is not active. This option works in Dec 1-25 only)");
}
}) ??
Command(args, Args("calendars"), _ => {
return () => {
var tsolversSelected = (
from tsolver in tsolvers
group tsolver by SolverExtensions.Year(tsolver) into g
orderby SolverExtensions.Year(g.First()) descending
select g.First()
).ToArray();
var solvers = GetSolvers(tsolversSelected);
foreach (var solver in solvers) {
solver.SplashScreen().Show();
}
};
}) ??
new Action(() => {
Console.WriteLine(Usage.Get());
});
try {
action();
} catch (AggregateException a) {
if (a.InnerExceptions.Count == 1 && a.InnerException is AocCommuncationError) {
Console.WriteLine(a.InnerException.Message);
} else {
throw;
}
}
Solver[] GetSolvers(params Type[] tsolver) {
return tsolver.Select(t => Activator.CreateInstance(t) as Solver).ToArray();
}
Action Command(string[] args, string[] regexes, Func<string[], Action> parse) {
if (args.Length != regexes.Length) {
return null;
}
var matches = Enumerable.Zip(args, regexes, (arg, regex) => new Regex("^" + regex + "$").Match(arg));
if (!matches.All(match => match.Success)) {
return null;
}
try {
return parse(matches.SelectMany(m =>
m.Groups.Count > 1 ? m.Groups.Cast<Group>().Skip(1).Select(g => g.Value)
: new[] { m.Value }
).ToArray());
} catch {
return null;
}
}
string[] Args(params string[] regex) {
return regex;
}
class Usage {
public static string Get() {
return $"""
Usage: dotnet run [arguments]
1) To run the solutions and admire your advent calendar:
[year]/[day|all] Solve the specified problems
today Shortcut to the above
[year] Solve the whole year
all Solve everything
calendars Show the calendars
2) To start working on new problems:
login to https://adventofcode.com, then copy your session cookie, and export
it in your console like this
export SESSION=73a37e9a72a...
then run the app with
update [year]/[day] Prepares a folder for the given day, updates the input,
the readme and creates a solution template.
update today Shortcut to the above.
3) To upload your answer:
set up your SESSION variable as above.
upload [year]/[day] Upload the answer for the selected year and day.
upload today Shortcut to the above.
4) Don't forget to tip the maintainer https://github.com/sponsors/encse.
""";
}
}