Updated runner regex to support multiple challenges per day, added day 1.
This commit is contained in:
parent
bf554aed7c
commit
601c19e02c
File diff suppressed because it is too large
Load Diff
13
runner.d
13
runner.d
|
@ -1,9 +1,10 @@
|
|||
#!/usr/local/bin/rdmd
|
||||
#!/usr/bin/rdmd
|
||||
|
||||
/**
|
||||
* Simple wrapper program for managing the various solutions. Each solution is
|
||||
* defined as a single D source file in `src/sX.d`, where `X` is the number of
|
||||
* the solution. All executables are placed in a `bin/` directory.
|
||||
* defined as a single D source file in `src/s\d+[a-z]*.d`, which is an "s"
|
||||
* followed by the day number, followed by a character representing which
|
||||
* challenge of the day it is.
|
||||
*
|
||||
* This script can be executed standalone via `./runner.d`, provided that you
|
||||
* have given the script the execution privilege (usually with `chmod +x runner.d`).
|
||||
|
@ -80,7 +81,7 @@ void clean() {
|
|||
int run(string[] args) {
|
||||
string[] solutionsToRun;
|
||||
if (args.length == 0 || args[0].strip().toLower() == "all") {
|
||||
auto r = ctRegex!(`^(s\d+)\.d$`);
|
||||
auto r = ctRegex!(`^(s\d+)[a-z]*\.d$`);
|
||||
foreach (entry; dirEntries("src", SpanMode.shallow, false)) {
|
||||
auto c = matchFirst(baseName(entry.name), r);
|
||||
if (c) solutionsToRun ~= c[1];
|
||||
|
@ -189,10 +190,10 @@ int create(string[] args) {
|
|||
return 1;
|
||||
}
|
||||
string solution = args[0].strip().toLower();
|
||||
auto r = ctRegex!(`^s\d+$`);
|
||||
auto r = ctRegex!(`^s\d+[a-z]*$`);
|
||||
auto c = matchFirst(solution, r);
|
||||
if (!c) {
|
||||
writefln!"Solution name \"%s\" is not valid. Should be \"s\\d+\"."(solution);
|
||||
writefln!"Solution name \"%s\" is not valid. Should be \"s\\d+[a-z]*\"."(solution);
|
||||
return 1;
|
||||
}
|
||||
string filePath = buildPath("src", solution ~ ".d");
|
||||
|
|
6
src/s1.d
6
src/s1.d
|
@ -1,6 +0,0 @@
|
|||
module s1;
|
||||
import util;
|
||||
|
||||
void main() {
|
||||
writeln("Hello world!!");
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
module s1a;
|
||||
import util;
|
||||
|
||||
void main() {
|
||||
ulong maxCalories = 0;
|
||||
ulong currentCalories = 0;
|
||||
foreach (line; File("input/1.txt", "r").byLine()) {
|
||||
if (line.strip().length == 0) {
|
||||
if (currentCalories > maxCalories) maxCalories = currentCalories;
|
||||
currentCalories = 0;
|
||||
} else {
|
||||
currentCalories += line.strip().to!ulong;
|
||||
}
|
||||
}
|
||||
writeln(maxCalories);
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
module s1b;
|
||||
import util;
|
||||
|
||||
void main() {
|
||||
ulong[] top3 = [0, 0, 0];
|
||||
ulong currentCalories = 0;
|
||||
foreach (line; File("input/1.txt", "r").byLine()) {
|
||||
if (line.strip().length == 0) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (currentCalories > top3[i]) {
|
||||
// Insert current into the top queue, then trim back to size.
|
||||
top3 = top3[0 .. i] ~ currentCalories ~ top3[i .. $];
|
||||
top3 = top3[0 ..3];
|
||||
break;
|
||||
}
|
||||
}
|
||||
currentCalories = 0;
|
||||
} else {
|
||||
currentCalories += line.strip().to!ulong;
|
||||
}
|
||||
}
|
||||
writeln(top3.sum());
|
||||
}
|
Loading…
Reference in New Issue