Added some comments to make le code clear.
This commit is contained in:
parent
d9f52222e4
commit
216eff5113
|
@ -7,6 +7,7 @@ void sonarSweep() {
|
||||||
int increaseCount = 0;
|
int increaseCount = 0;
|
||||||
auto values = readInts("source/day1/input.txt");
|
auto values = readInts("source/day1/input.txt");
|
||||||
int previousValue = values[0];
|
int previousValue = values[0];
|
||||||
|
// Iterate from value index 1 to the end of the list.
|
||||||
foreach(value; values[1..$]) {
|
foreach(value; values[1..$]) {
|
||||||
if (value > previousValue) {
|
if (value > previousValue) {
|
||||||
increaseCount++;
|
increaseCount++;
|
||||||
|
|
|
@ -7,6 +7,7 @@ void slidingSum() {
|
||||||
int[] values = readInts("source/day1/input.txt");
|
int[] values = readInts("source/day1/input.txt");
|
||||||
int previousSum = values[0] + values[1] + values[2];
|
int previousSum = values[0] + values[1] + values[2];
|
||||||
int increaseCount = 0;
|
int increaseCount = 0;
|
||||||
|
// Iterate over indices 1 to 2 minus the length of the array.
|
||||||
foreach (i; 1 .. (values.length - 2)) {
|
foreach (i; 1 .. (values.length - 2)) {
|
||||||
int currentSum = values[i] + values[i + 1] + values[i + 2];
|
int currentSum = values[i] + values[i + 1] + values[i + 2];
|
||||||
if (currentSum > previousSum) increaseCount++;
|
if (currentSum > previousSum) increaseCount++;
|
||||||
|
|
|
@ -5,12 +5,17 @@ import std.stdio;
|
||||||
import std.string;
|
import std.string;
|
||||||
import std.conv;
|
import std.conv;
|
||||||
import std.typecons;
|
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();
|
auto parts = op.split();
|
||||||
int x = parts[1].to!int;
|
return tuple(parts[0].to!string, parts[1].to!int);
|
||||||
string d = parts[0].to!string;
|
|
||||||
return tuple(d, x);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void dive() {
|
void dive() {
|
||||||
|
|
|
@ -6,6 +6,12 @@ import std.algorithm;
|
||||||
import std.array;
|
import std.array;
|
||||||
import std.conv;
|
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) {
|
int[] readInts(string filename) {
|
||||||
return readText("source/day1/input.txt")
|
return readText("source/day1/input.txt")
|
||||||
.split("\n")
|
.split("\n")
|
||||||
|
|
Loading…
Reference in New Issue