Added day 1.

This commit is contained in:
Andrew Lalis 2021-12-01 08:39:14 +01:00
parent 5d7041d956
commit 564c09511c
7 changed files with 2085 additions and 0 deletions

15
.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/adventofcode2021
adventofcode2021.so
adventofcode2021.dylib
adventofcode2021.dll
adventofcode2021.a
adventofcode2021.lib
adventofcode2021-test-*
*.exe
*.o
*.obj
*.lst

9
dub.json Normal file
View File

@ -0,0 +1,9 @@
{
"authors": [
"Andrew Lalis"
],
"copyright": "Copyright © 2021, Andrew Lalis",
"description": "Advent of Code 2021 attempts.",
"license": "MIT",
"name": "adventofcode2021"
}

8
source/app.d Normal file
View File

@ -0,0 +1,8 @@
import std.stdio;
import day1.part1;
import day1.part2;
void main() {
slidingSum();
}

2000
source/day1/input.txt Normal file

File diff suppressed because it is too large Load Diff

17
source/day1/part1.d Normal file
View File

@ -0,0 +1,17 @@
module day1.part1;
import std.stdio;
import util.fileutils;
void sonarSweep() {
int increaseCount = 0;
auto values = readInts("source/day1/input.txt");
int previousValue = values[0];
foreach(value; values[1..$]) {
if (value > previousValue) {
increaseCount++;
}
previousValue = value;
}
writefln("%d", increaseCount);
}

20
source/day1/part2.d Normal file
View File

@ -0,0 +1,20 @@
module day1.part2;
import std.stdio;
import util.fileutils;
void slidingSum() {
int[] values = readInts("source/day1/input.txt");
int previousSum = values[0] + values[1] + values[2];
int increaseCount = 0;
foreach (i; 1 .. (values.length - 2)) {
int currentSum = values[i] + values[i + 1] + values[i + 2];
bool increase = false;
if (currentSum > previousSum) {
increaseCount++;
increase = true;
}
previousSum = currentSum;
}
writefln("%d", increaseCount);
}

16
source/util/fileutils.d Normal file
View File

@ -0,0 +1,16 @@
module util.fileutils;
import std.file;
import std.string;
import std.algorithm;
import std.array;
import std.conv;
int[] readInts(string filename) {
return readText("source/day1/input.txt")
.split("\n")
.map!(s => s.strip())
.filter!(s => s.length > 0)
.map!(s => s.to!int)
.array;
}