Updated runner regex to support multiple challenges per day, added day 1.

This commit is contained in:
Andrew Lalis 2022-12-01 07:35:23 +01:00
parent bf554aed7c
commit 601c19e02c
5 changed files with 2295 additions and 12 deletions

2249
input/1.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,10 @@
#!/usr/local/bin/rdmd #!/usr/bin/rdmd
/** /**
* Simple wrapper program for managing the various solutions. Each solution is * 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 * defined as a single D source file in `src/s\d+[a-z]*.d`, which is an "s"
* the solution. All executables are placed in a `bin/` directory. * 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 * 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`). * have given the script the execution privilege (usually with `chmod +x runner.d`).
@ -80,7 +81,7 @@ void clean() {
int run(string[] args) { int run(string[] args) {
string[] solutionsToRun; string[] solutionsToRun;
if (args.length == 0 || args[0].strip().toLower() == "all") { 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)) { foreach (entry; dirEntries("src", SpanMode.shallow, false)) {
auto c = matchFirst(baseName(entry.name), r); auto c = matchFirst(baseName(entry.name), r);
if (c) solutionsToRun ~= c[1]; if (c) solutionsToRun ~= c[1];
@ -189,10 +190,10 @@ int create(string[] args) {
return 1; return 1;
} }
string solution = args[0].strip().toLower(); string solution = args[0].strip().toLower();
auto r = ctRegex!(`^s\d+$`); auto r = ctRegex!(`^s\d+[a-z]*$`);
auto c = matchFirst(solution, r); auto c = matchFirst(solution, r);
if (!c) { 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; return 1;
} }
string filePath = buildPath("src", solution ~ ".d"); string filePath = buildPath("src", solution ~ ".d");

View File

@ -1,6 +0,0 @@
module s1;
import util;
void main() {
writeln("Hello world!!");
}

16
src/s1a.d Normal file
View File

@ -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);
}

23
src/s1b.d Normal file
View File

@ -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());
}