1
0
Fork 0
mirror of https://github.com/HenkKalkwater/aoc-2020 synced 2024-06-02 18:42:42 +00:00
aoc-2020/source/app.d

171 lines
3.7 KiB
D
Raw Permalink Normal View History

2020-12-01 11:28:18 +00:00
import core.stdc.stdlib;
import std.conv;
import std.exception;
2020-12-01 11:28:18 +00:00
import std.format;
import std.getopt;
2020-12-10 08:37:13 +00:00
import std.range;
import std.stdio;
import std.variant;
2020-12-01 11:28:18 +00:00
import day1;
2020-12-02 16:53:43 +00:00
import day2;
2020-12-03 09:50:55 +00:00
import day3;
2020-12-05 02:57:30 +00:00
import day4;
import day5;
2020-12-06 10:49:20 +00:00
import day6;
2020-12-07 07:03:29 +00:00
import day7;
2020-12-08 05:38:59 +00:00
import day8;
import day9;
2020-12-10 08:58:18 +00:00
import day10;
2020-12-11 07:29:43 +00:00
import day11;
2020-12-12 11:15:24 +00:00
import day12;
2020-12-13 10:39:50 +00:00
import day13;
2020-12-14 06:04:16 +00:00
import day14;
2020-12-15 06:36:42 +00:00
import day15;
2020-12-16 14:12:54 +00:00
import day16;
2020-12-17 22:50:41 +00:00
import day17;
2020-12-01 14:42:11 +00:00
import dayutil;
2020-12-01 11:28:18 +00:00
immutable string progName = "aoc-2020";
2020-12-10 08:37:13 +00:00
version(Unix) {
extern(C) int isatty(int);
}
Variant function(int, File, bool, string[])[] programs = [
2020-12-02 16:53:43 +00:00
&day1.run,
2020-12-03 09:50:55 +00:00
&day2.run,
&day3.run,
2020-12-05 02:57:30 +00:00
&day4.run,
&day5.run,
2020-12-06 10:49:20 +00:00
&day6.run,
2020-12-07 07:03:29 +00:00
&day7.run,
2020-12-08 05:38:59 +00:00
&day8.run,
&day9.run,
2020-12-10 08:58:18 +00:00
&day10.run,
2020-12-11 07:29:43 +00:00
&day11.run,
2020-12-12 11:15:24 +00:00
&day12.run,
2020-12-13 10:39:50 +00:00
&day13.run,
2020-12-14 06:04:16 +00:00
&day14.run,
2020-12-15 06:36:42 +00:00
&day15.run,
2020-12-16 14:12:54 +00:00
&day16.run,
2020-12-17 22:50:41 +00:00
&day17.run,
2020-12-01 11:28:18 +00:00
];
2020-12-10 08:37:13 +00:00
void printUsage(string message = null) {
import core.runtime;
string name = Runtime.args[0];
stderr.writeln("USAGE: %s [OPTIONS...] DAY PART [INPUT_FILE] (to run a specific day)".format(name));
stderr.writeln(" OR: %s [OPTIONS...] all (to run each day)".format(name));
stderr.writeln();
stderr.writeln(" DAY = int between 1 and 25 (inclusive), specifiying the day to run");
stderr.writeln(" PART = int between 1 and 2 (inclusive), specifiying the part to run");
stderr.writeln(" INPUT_FILE = file to read from. '-' for stdin. If not specified, use in/[DAY].txt");
stderr.writeln();
stderr.writeln("OPTIONS:");
stderr.writeln(" --bigboy, -B = use bigboy challenges");
stderr.writeln(" --benchmark, -b = record time it took to execute ");
2020-12-01 14:42:11 +00:00
if (message != null) {
2020-12-10 08:37:13 +00:00
stderr.writeln();
2020-12-01 14:42:11 +00:00
stderr.writeln(message);
}
exit(-1);
}
2020-12-10 08:37:13 +00:00
File getDefaultFile(int day, bool bigboy) {
if (bigboy) {
return File("in/bigboy/%d.txt".format(day), "rb");
} else {
return File("in/%d.txt".format(day), "rb");
}
}
2020-12-01 11:28:18 +00:00
void main(string[] args) {
bool bigboy = false;
2020-12-10 08:37:13 +00:00
bool benchmark = false;
2020-12-01 11:28:18 +00:00
try {
2020-12-10 08:37:13 +00:00
auto opts = getopt(args,
"bigboy|B", &bigboy,
"benchmark|b", &benchmark);
if (opts.helpWanted) {
printUsage();
}
} catch (GetOptException e) {
printUsage(e.msg);
2020-12-01 11:28:18 +00:00
}
2020-12-10 08:37:13 +00:00
if (args.length < 2) {
printUsage();
2020-12-01 11:28:18 +00:00
}
2020-12-10 08:37:13 +00:00
bool runAll = false;
if (args[1] == "all") {
/+version(Posix) {
bool tty = cast(bool) isatty(stdout.fileNo);
} else {+/
bool tty = false;
//}
runAll = true;
writeln(tty);
writeln("STATUS\tDAY\tPART\tRESULT");
foreach (dayNo, day; programs.enumerate(1)) {
File file = getDefaultFile(dayNo, bigboy);
foreach(part; 1..3) {
if (tty) {
write("RUNNING\t%d\t%d\t...\r".format(dayNo, part));
stdout.flush();
}
Variant result = day(part, file, bigboy, []);
writeln("DONE \t%d\t%d\t%s ".format(dayNo, part, std.conv.to!string(result)));
file.rewind();
}
}
} else {
if (args.length < 3) {
printUsage();
}
2020-12-10 08:37:13 +00:00
int day;
try {
day = to!int(args[1]);
} catch (ConvException e) {
printUsage("DAY is not an integer");
}
if (day <= 0 || day > programs.length) {
printUsage("DAY must be between 1 and %d".format(programs.length));
}
int part;
try {
part = to!int(args[2]);
} catch (ConvException e) {
printUsage("PART is not an integer");
}
File file;
try {
if (args.length < 4) {
file = getDefaultFile(day, bigboy);
} else if(args[3] == "-") {
file = stdin;
} else {
2020-12-10 08:37:13 +00:00
file = File(args[3], "rb");
}
2020-12-10 08:37:13 +00:00
} catch (ErrnoException e) {
printUsage("Error %d while opening input file: %s".format(e.errno, e.message));
}
try {
Variant result = programs[day - 1](part, file, bigboy, args[3..$]);
writeln(result);
} catch(ArgumentException e) {
printUsage(e.msg);
} catch(Exception e) {
stderr.writeln("Fatal error occurred: " ~ e.msg);
exit(-1);
}
}
2020-11-30 14:22:15 +00:00
}