From 3533c418cf220e3ecb14fbf7481f613d6c87db22 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 1 Dec 2022 13:18:29 +0100 Subject: [PATCH] Cleaned up the logic. --- runner.d | 8 ++++---- src/s1a.d | 16 ++++------------ src/s1b.d | 21 +++------------------ src/util.d | 1 + 4 files changed, 12 insertions(+), 34 deletions(-) diff --git a/runner.d b/runner.d index 75851bc..7ddb114 100755 --- a/runner.d +++ b/runner.d @@ -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); } diff --git a/src/s1a.d b/src/s1a.d index 386f006..8364ab3 100644 --- a/src/s1a.d +++ b/src/s1a.d @@ -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); -} \ No newline at end of file + readText("input/1.txt").strip.splitter("\n\n") + .map!(c => c.strip.splitter("\n").map!(l => l.to!ulong).sum) + .maxElement.writeln; +} diff --git a/src/s1b.d b/src/s1b.d index bff2d66..38c3423 100644 --- a/src/s1b.d +++ b/src/s1b.d @@ -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; } diff --git a/src/util.d b/src/util.d index bcf797c..4fdeacd 100644 --- a/src/util.d +++ b/src/util.d @@ -10,3 +10,4 @@ public import std.algorithm; public import std.conv; public import std.path; public import std.uni; +public import std.array;