-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.cpp
74 lines (58 loc) · 1.52 KB
/
day01.cpp
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
/**
* @file day01.cpp
* @author Miguel Ángel Moreno Castro
* @date Dec 1, 2022
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
vector<int> Read(const string & file){
ifstream fi(file);
string temp;
vector<int> elves;
int sum = 0;
if (fi.is_open()){
while (!fi.eof()){
getline(fi, temp, '\n');
if (!temp.empty()){
sum += stoi(temp);
} else {
elves.push_back(sum);
sum = 0;
}
}
}
fi.close();
return elves;
}
int Puzzle1(const vector<int> & elves){
int max = elves.at(0);
for (int i = 1; i < elves.size(); i++){
if (max < elves.at(i))
max = elves.at(i);
}
return max;
}
int Puzzle2(const vector<int> & elves){
int max1, max2, max3;
max1 = Puzzle1(elves);
max2 = elves.at(0) > elves.at(1) ? elves.at(0) : elves.at(1);
max3 = max2 == elves.at(0) ? elves.at(1) : elves.at(0);
for (int i = 2; i < elves.size(); i++){
if (max2 < elves.at(i) && elves.at(i) < max1){
max3 = max2;
max2 = elves.at(i);
}
}
return max1 + max2 + max3;
}
int main(int argc, char * argv[]){
if (argc != 2)
exit(1);
vector<int> elves = Read(argv[1]);
cout << "\nThe maximum amount of calories carried by an elf is " << Puzzle1(elves);
cout << "\nThe total amount of calories carried by the top three elves is " << Puzzle2(elves);
return 0;
}