Cleaned up the logic.

This commit is contained in:
Andrew Lalis 2022-12-01 13:18:29 +01:00
parent 601c19e02c
commit 3533c418cf
4 changed files with 12 additions and 34 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/rdmd #!/usr/bin/env rdmd
/** /**
* Simple wrapper program for managing the various solutions. Each solution is * Simple wrapper program for managing the various solutions. Each solution is
@ -81,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+)[a-z]*\.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];
@ -168,10 +168,10 @@ int compileSolution(string solution) {
*/ */
void runSolution(string solution) { void runSolution(string solution) {
string processPath = buildPath("bin", solution); string processPath = buildPath("bin", solution);
writefln!"-----< %s >-----\n"(solution); writefln!"-----< %s >-----"(solution);
Pid pid = spawnProcess(processPath); Pid pid = spawnProcess(processPath);
int result = wait(pid); int result = wait(pid);
writefln!"\n-----< %s >-----\n"(solution); writeln();
if (result != 0) { if (result != 0) {
writefln!"Solution %s failed with exit code %d."(solution, result); writefln!"Solution %s failed with exit code %d."(solution, result);
} }

View File

@ -2,15 +2,7 @@ module s1a;
import util; import util;
void main() { void main() {
ulong maxCalories = 0; readText("input/1.txt").strip.splitter("\n\n")
ulong currentCalories = 0; .map!(c => c.strip.splitter("\n").map!(l => l.to!ulong).sum)
foreach (line; File("input/1.txt", "r").byLine()) { .maxElement.writeln;
if (line.strip().length == 0) { }
if (currentCalories > maxCalories) maxCalories = currentCalories;
currentCalories = 0;
} else {
currentCalories += line.strip().to!ulong;
}
}
writeln(maxCalories);
}

View File

@ -2,22 +2,7 @@ module s1b;
import util; import util;
void main() { void main() {
ulong[] top3 = [0, 0, 0]; readText("input/1.txt").strip.splitter("\n\n")
ulong currentCalories = 0; .map!(c => c.strip.splitter("\n").map!(l => l.to!ulong).sum)
foreach (line; File("input/1.txt", "r").byLine()) { .array.topN!((a, b) => a > b)(3).sum.writeln;
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());
} }

View File

@ -10,3 +10,4 @@ public import std.algorithm;
public import std.conv; public import std.conv;
public import std.path; public import std.path;
public import std.uni; public import std.uni;
public import std.array;