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
@ -81,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+)[a-z]*\.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];
@ -168,10 +168,10 @@ int compileSolution(string solution) {
*/
void runSolution(string solution) {
string processPath = buildPath("bin", solution);
writefln!"-----< %s >-----\n"(solution);
writefln!"-----< %s >-----"(solution);
Pid pid = spawnProcess(processPath);
int result = wait(pid);
writefln!"\n-----< %s >-----\n"(solution);
writeln();
if (result != 0) {
writefln!"Solution %s failed with exit code %d."(solution, result);
}

View File

@ -2,15 +2,7 @@ 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);
readText("input/1.txt").strip.splitter("\n\n")
.map!(c => c.strip.splitter("\n").map!(l => l.to!ulong).sum)
.maxElement.writeln;
}

View File

@ -2,22 +2,7 @@ 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());
readText("input/1.txt").strip.splitter("\n\n")
.map!(c => c.strip.splitter("\n").map!(l => l.to!ulong).sum)
.array.topN!((a, b) => a > b)(3).sum.writeln;
}

View File

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