Added day 1.
This commit is contained in:
parent
5d7041d956
commit
564c09511c
|
@ -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
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"authors": [
|
||||
"Andrew Lalis"
|
||||
],
|
||||
"copyright": "Copyright © 2021, Andrew Lalis",
|
||||
"description": "Advent of Code 2021 attempts.",
|
||||
"license": "MIT",
|
||||
"name": "adventofcode2021"
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import std.stdio;
|
||||
|
||||
import day1.part1;
|
||||
import day1.part2;
|
||||
|
||||
void main() {
|
||||
slidingSum();
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue