-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.cpp
More file actions
70 lines (60 loc) · 1.86 KB
/
Copy pathday1.cpp
File metadata and controls
70 lines (60 loc) · 1.86 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <array>
std::string::size_type sz; // Size of input ints. alias of size_t (unisgned int)
const std::string inputPath = "inputs/input1.txt";
int main(){
// Ex1
std::ifstream ifs(inputPath, std::ifstream::in);
int currentCalorieCount = 0;
int maxCalories = 0;
std::string line;
while(std::getline(ifs, line)){ // stops at eof
if (line != ""){
currentCalorieCount += std::stoi(line, &sz);
}
else{
if (currentCalorieCount > maxCalories){
maxCalories = currentCalorieCount;
}
currentCalorieCount = 0;
}
}
ifs.close();
std::cout << "--Ex1 Output " << maxCalories << std::endl;
// Ex2
std::array<int, 3> topThreeCaloriesCounts = {0, 0, 0};
ifs.open(inputPath, std::ifstream::in);
currentCalorieCount = 0;
while(std::getline(ifs, line)){ // stops at eof
if (line != ""){
currentCalorieCount += std::stoi(line, &sz);
}
else{
// getMinCaloriCountFromTop
int min = INT_MAX;
int minIdx;
for (int j = 0; j < topThreeCaloriesCounts.size(); j++)
{
if (topThreeCaloriesCounts[j] < min)
{
min = topThreeCaloriesCounts[j];
minIdx = j;
}
}
if (currentCalorieCount > topThreeCaloriesCounts[minIdx])
{
topThreeCaloriesCounts[minIdx] = currentCalorieCount;
}
currentCalorieCount = 0;
}
}
ifs.close();
int fullCalorySum = 0;
for(int i=0; i < topThreeCaloriesCounts.size();i++){
fullCalorySum += topThreeCaloriesCounts[i];
}
std::cout << "--Ex2 Output " << fullCalorySum << std::endl;
return 0;
}