-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (91 loc) · 3.02 KB
/
main.cpp
File metadata and controls
104 lines (91 loc) · 3.02 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
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
#include <cstdio>
#include <cstdlib>
#include <puzzles.h>
#include <cstring>
#include "tester.h"
long year = -1, day = -1;
bool do_print = true;
auto get_answer_path(int year) {
char filename[512] = {};
sprintf(filename, "%s/../puzzles/%d/answers.txt", ox::executable_folder().c_str(), year);
return std::string(filename);
}
static const char base[] =
R"COMMAND(python -c "import aocd; aocd.submit(%s, part='%c', day=%d, year=%d)" 2>/dev/null)COMMAND";
using unique_process = std::unique_ptr<FILE, decltype([](FILE* f) { pclose(f); })>;
void submit(const answertype& ans, int year, int day, int part) {
char command[sizeof(base) + 20];
char read_output[1024];
auto str_ans = std::visit(answer_to_string2{}, ans);
if (!str_ans)
return;
sprintf(command, base, str_ans->c_str(), 'a' - 1 + part, day, year);
unique_process f{popen(command, "r")};
while (fgets(read_output, sizeof read_output, f.get()) != nullptr) {
std::string_view temp = read_output;
if (temp.contains("\033[31m")) {
auto endofsentence = temp.find('.');
std::string substring{temp.substr(0, endofsentence)};
printf("%s\n\033[0m", substring.c_str());
return;
}
if (temp.contains("incorrect")) {
printf("\033[31m%s\n\033[0m", read_output);
return;
}
}
printf("\033[32mThe answer is correct :)\033[0m\n");
}
int main(int argc, const char** argv) {
const char* filename = nullptr;
bool do_test = false;
bool do_submit = false;
int day = -1, year = -1;
int part = 0b11;
for (int i = 1; i < argc; ++i) {
char* end;
int argument = static_cast<int>(strtol(argv[i], &end, 10));
if (*end != 0) {
if (!strcmp(argv[i], "test")) {
do_test = true;
} else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--part")) {
++i;
part = atoi(argv[i]);
} else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--do_submit")) {
do_submit = true;
} else {
filename = argv[i];
}
continue;
}
(argument < 1000 ? day : year) = argument;
}
year = year < 0 ? pseudo_puzzle_array::current_year() : year;
if (do_test) {
do_print = false;
std::string answer_path = get_answer_path(year);
test(year, puzzles[year], answer_path);
return 0;
}
filename = filename ?: "";
if (day < 0) {
printf("ERROR: No day given");
exit(1);
}
printf("Year %02d, Day %02d:\n", year, day);
puzzle_options opt{.day = day, .year = year, .filename = filename};
auto puzzle = puzzles[year][day - 1];
if (part & 0b01) {
auto ans = puzzle.first(opt);
if (do_submit) {
submit(ans, year, day, 1);
}
}
if (part & 0b10) {
auto ans = puzzle.second(opt);
if (do_submit) {
submit(ans, year, day, 2);
}
}
return 0;
}