2020-12-01 11:28:18 +00:00
|
|
|
import core.stdc.stdlib;
|
|
|
|
|
|
|
|
import std.conv;
|
2020-12-05 12:03:47 +00:00
|
|
|
import std.exception;
|
2020-12-01 11:28:18 +00:00
|
|
|
import std.format;
|
2020-11-30 14:22:15 +00:00
|
|
|
import std.stdio;
|
2020-12-01 11:28:18 +00:00
|
|
|
import std.getopt;
|
2020-12-05 11:16:58 +00:00
|
|
|
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;
|
2020-12-05 11:16:58 +00:00
|
|
|
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;
|
2020-12-09 14:48:02 +00:00
|
|
|
import day9;
|
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-07 05:05:35 +00:00
|
|
|
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,
|
2020-12-05 11:16:58 +00:00
|
|
|
&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,
|
2020-12-09 14:48:02 +00:00
|
|
|
&day9.run,
|
2020-12-01 11:28:18 +00:00
|
|
|
];
|
|
|
|
|
2020-12-01 14:42:11 +00:00
|
|
|
void printUsage(string name) {
|
|
|
|
printUsage(name, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printUsage(string name, string message) {
|
2020-12-05 12:03:47 +00:00
|
|
|
stderr.writeln("USAGE: %s [DAY] [PART] <INPUT_FILE> ".format(name));
|
|
|
|
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. '-' or missing implies stdin");
|
2020-12-01 14:42:11 +00:00
|
|
|
if (message != null) {
|
|
|
|
stderr.writeln(message);
|
|
|
|
}
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:28:18 +00:00
|
|
|
void main(string[] args) {
|
2020-12-07 05:05:35 +00:00
|
|
|
bool bigboy = false;
|
2020-12-01 11:28:18 +00:00
|
|
|
int day;
|
2020-12-07 05:05:35 +00:00
|
|
|
getopt(args,
|
|
|
|
"bigboy", "Execute the so-named 'biboy' variant of this exercise", &bigboy);
|
2020-12-05 11:16:58 +00:00
|
|
|
if (args.length < 3) {
|
2020-12-01 14:42:11 +00:00
|
|
|
printUsage(args[0]);
|
2020-12-01 11:28:18 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
day = to!int(args[1]);
|
|
|
|
} catch (ConvException e) {
|
2020-12-01 14:42:11 +00:00
|
|
|
printUsage(args[0], "[day] is not an integer");
|
2020-12-01 11:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (day <= 0 || day > programs.length) {
|
2020-12-06 10:49:20 +00:00
|
|
|
printUsage(args[0], "[day] must be between 1 and %d".format(programs.length));
|
2020-12-01 11:28:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:16:58 +00:00
|
|
|
int part;
|
|
|
|
try {
|
|
|
|
part = to!int(args[2]);
|
|
|
|
} catch (ConvException e) {
|
|
|
|
printUsage(args[0], "[part] is not an integer");
|
|
|
|
}
|
2020-12-05 12:03:47 +00:00
|
|
|
|
|
|
|
File file;
|
2020-12-09 14:48:02 +00:00
|
|
|
try {
|
|
|
|
if (args.length < 4) {
|
|
|
|
if (bigboy) {
|
|
|
|
file = File("in/bigboy/%d.txt".format(day), "rb");
|
|
|
|
} else {
|
|
|
|
file = File("in/%d.txt".format(day), "rb");
|
|
|
|
}
|
|
|
|
} else if(args[3] == "-") {
|
|
|
|
file = stdin;
|
|
|
|
} else {
|
2020-12-05 12:03:47 +00:00
|
|
|
file = File(args[3], "rb");
|
|
|
|
}
|
2020-12-09 14:48:02 +00:00
|
|
|
} catch (ErrnoException e) {
|
|
|
|
printUsage(args[0], "Error %d while opening input file: %s".format(e.errno, e.message));
|
2020-12-05 12:03:47 +00:00
|
|
|
}
|
2020-12-01 11:28:18 +00:00
|
|
|
|
2020-12-01 14:42:11 +00:00
|
|
|
try {
|
2020-12-07 05:05:35 +00:00
|
|
|
Variant result = programs[day - 1](part, file, bigboy, args[3..$]);
|
2020-12-05 11:16:58 +00:00
|
|
|
writeln(result);
|
2020-12-01 14:42:11 +00:00
|
|
|
} catch(ArgumentException e) {
|
|
|
|
printUsage(args[0], e.msg);
|
|
|
|
}
|
2020-11-30 14:22:15 +00:00
|
|
|
|
|
|
|
}
|