diff --git a/source/day1/part1.d b/source/day1/part1.d index 17edeb4..ecd335c 100644 --- a/source/day1/part1.d +++ b/source/day1/part1.d @@ -7,6 +7,7 @@ void sonarSweep() { int increaseCount = 0; auto values = readInts("source/day1/input.txt"); int previousValue = values[0]; + // Iterate from value index 1 to the end of the list. foreach(value; values[1..$]) { if (value > previousValue) { increaseCount++; diff --git a/source/day1/part2.d b/source/day1/part2.d index 4b07f64..a628c24 100644 --- a/source/day1/part2.d +++ b/source/day1/part2.d @@ -7,6 +7,7 @@ void slidingSum() { int[] values = readInts("source/day1/input.txt"); int previousSum = values[0] + values[1] + values[2]; int increaseCount = 0; + // Iterate over indices 1 to 2 minus the length of the array. foreach (i; 1 .. (values.length - 2)) { int currentSum = values[i] + values[i + 1] + values[i + 2]; if (currentSum > previousSum) increaseCount++; diff --git a/source/day2/part1and2.d b/source/day2/part1and2.d index dbd1284..3d47859 100644 --- a/source/day2/part1and2.d +++ b/source/day2/part1and2.d @@ -5,12 +5,17 @@ import std.stdio; import std.string; import std.conv; import std.typecons; +import std.traits; -Tuple!(string, int) parseOp(char[] op) { +/** + * Parses a submarine instruction. + * Params: + * op = The operation to parse. + * Returns: A tuple containing the operation string and the value. + */ +Tuple!(string, int) parseOp(S)(S op) @safe pure if (isSomeString!S) { auto parts = op.split(); - int x = parts[1].to!int; - string d = parts[0].to!string; - return tuple(d, x); + return tuple(parts[0].to!string, parts[1].to!int); } void dive() { diff --git a/source/util/fileutils.d b/source/util/fileutils.d index 3a12b9c..3530b49 100644 --- a/source/util/fileutils.d +++ b/source/util/fileutils.d @@ -6,6 +6,12 @@ import std.algorithm; import std.array; import std.conv; +/** + * Reads a list of integers from a file, assuming one integer per line. + * Params: + * filename = The name of the file to read, relative to the working dir. + * Returns: A list of integers. + */ int[] readInts(string filename) { return readText("source/day1/input.txt") .split("\n")