-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.d
139 lines (117 loc) · 3.31 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import std.algorithm: map, maxIndex, maxElement, topN, sum;
import std.array: array, empty;
import std.conv: to;
import std.exception: assertThrown, assertNotThrown, enforce;
import std.file: exists;
import std.range: back;
import std.regex: ctRegex, matchFirst;
import std.stdio: File, writeln, stdin;
/// Checks whenever given line contains valid calories values or empty line.
string validator(string line)
{
auto rExpr = ctRegex!"^[0-9]*$";
enforce(matchFirst(line, rExpr).length > 0, "not valid calories data: " ~ line);
return line;
}
///
unittest
{
assertNotThrown(validator(""));
assertNotThrown(validator("1234"));
assertThrown(validator("a"));
assertThrown(validator("."));
}
/// Extracts lines with calories from either filename or stdin
string[] getLines(string fname)
{
File handle = (() => !fname.empty() && fname.exists() ? File(fname, "r") : stdin)();
return handle.byLineCopy()
.map!validator()
.array();
}
///
unittest
{
assert(getLines("test_input.txt").length == 14);
}
/// Parse lines and extracts
uint[] extractElvesCalories(string[] data)
{
uint[] caloriesPerElf;
uint caloriesSoFar = 0;
foreach (line; data)
{
if (line.empty())
{
caloriesPerElf ~= caloriesSoFar;
caloriesSoFar = 0;
}
else
{
caloriesSoFar += line.to!uint();
}
}
if (!data.empty() && !data.back.empty())
{
caloriesPerElf ~= caloriesSoFar;
}
return caloriesPerElf;
}
unittest
{
assert(extractElvesCalories([]) == []);
assert(extractElvesCalories(["1", "2"]) == [3]);
assert(extractElvesCalories(["1", "2", ""]) == [3]);
assert(extractElvesCalories(["1", "2", "", "4"]) == [3, 4]);
assert(extractElvesCalories(["1", "2", "", "4", ""]) == [3, 4]);
}
size_t findMostJuicedElf(uint[] calories)
{
return calories.maxIndex() + 1;
}
unittest
{
assert(findMostJuicedElf([]) == 0);
assert(findMostJuicedElf([0]) == 1);
assert(findMostJuicedElf([100]) == 1);
assert(findMostJuicedElf([100, 1000, 500]) == 2);
}
uint findMostCalories(uint[] calories)
{
return !calories.empty() ? calories.maxElement() : 0;
}
unittest
{
assert(findMostCalories([]) == 0);
assert(findMostCalories([0]) == 0);
assert(findMostCalories([100]) == 100);
assert(findMostCalories([100, 1000, 500]) == 1000);
}
uint findMostCaloriesFromTop3(uint[] calories)
{
return calories.topN!"a > b"(3).sum();
}
unittest
{
assert(findMostCaloriesFromTop3([]) == 0);
assert(findMostCaloriesFromTop3([1000]) == 1000);
assert(findMostCaloriesFromTop3([1000, 2000]) == 3000);
assert(findMostCaloriesFromTop3([1000, 2000, 3000, 300, 200, 100]) == 6000);
}
void main(string[] args)
{
string[] lines = getLines(args.length > 1 ? args[1] : "");
uint[] calories = extractElvesCalories(lines);
writeln("most calories: ", findMostCalories(calories));
writeln("sum of top3 calories: ", findMostCaloriesFromTop3(calories));
}
unittest
{
string[] lines = getLines("test_input.txt");
assert(lines.length == 14);
uint[] calories = extractElvesCalories(lines);
assert(calories.length == 5);
assert(findMostJuicedElf(calories) == 4);
assert(findMostCalories(calories) == 24_000);
assert(findMostCaloriesFromTop3(calories) == 45_000);
}