-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.d
61 lines (48 loc) · 1.18 KB
/
day01.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module day01;
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main()
{
int[] depths = File("input/day01.txt")
.byLine()
.map!(to!string)
.map!strip
.filter!(line => line.length > 0)
.map!(to!int)
.array();
// pt 1
depth_increases(depths, 1);
// pt 2
depth_increases(depths, 3);
}
void depth_increases(int[] depths, int window_size)
{
int increases = 0;
int decreases = 0;
int[] prev_window = depths[0 .. window_size];
int[] curr_window = depths[1 .. 1 + window_size];
for (int i = 2; i <= depths.length; i++)
{
int prev_sum = prev_window.fold!((a, b) => a + b);
int curr_sum = curr_window.fold!((a, b) => a + b);
// writeln(prev_sum, " ", curr_sum);
if (prev_sum < curr_sum)
{
increases++;
}
else if (prev_sum > curr_sum)
{
decreases++;
}
if (i + window_size > depths.length)
{
break;
}
prev_window = curr_window;
curr_window = depths[i .. i + window_size];
}
writeln(increases.to!string);
}