Files
contests/advent_of_code/2022/1/main.cpp

54 lines
1.4 KiB
C++

#include <bits/stdc++.h>
using namespace std;
int main(){
ifstream input_file("input.txt");
string line;
int max_energy = 0;
// if(input_file.is_open()){
// int cur_energy = 0;
// while(getline(input_file, line)){
// if(line.length() > 1){
// cur_energy += stoi(line);
// } else{
// max_energy = max(cur_energy, max_energy);
// cur_energy = 0;
// };
// }
// }
if(input_file.is_open()){
int energy_max = 0, energy_mid = 0, energy_low = 0;
int cur_energy = 0;
while(getline(input_file, line)){
if(line.length() > 1){
cur_energy += stoi(line);
} else{
if(cur_energy > energy_max){
energy_low = energy_mid;
energy_mid = energy_max;
energy_max = cur_energy;
} else if(cur_energy > energy_mid){
energy_low = energy_mid;
energy_mid = cur_energy;
} else if(cur_energy > energy_low){
energy_low = cur_energy;
}
cur_energy = 0;
}
}
max_energy = energy_max + energy_mid + energy_low;
}
cout << "Max. energy: " << max_energy << endl;
return 0;
}