advent_of_code_2021/01/main.cpp
2021-12-01 07:33:08 +01:00

56 lines
1.3 KiB
C++

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
std::vector<int> getDepths(const std::string &file_name) {
std::ifstream file(file_name);
std::vector<int> result{};
int tmp = 0;
std::string str;
while (std::getline(file, str)) {
std::stringstream ss(str);
ss >> tmp;
result.push_back(tmp);
}
return result;
}
int part1(const std::vector<int> &input) {
int result = 0;
int tmp = input[0];
for (auto &depth : input) {
if (depth > tmp) {
result++;
}
tmp = depth;
}
return result;
}
int part2(const std::vector<int> &input) {
int result = 0;
int tmp = input[0] + input[1] + input[2];
for (size_t i = 3; i < input.size(); i++) {
auto window = input[i] + input[i - 1] + input[i - 2];
if (window > tmp) {
result++;
}
tmp = window;
}
return result;
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "You must provide input file!" << std::endl;
return 1;
}
auto depths = getDepths(argv[1]);
std::cout << "There are \033[91;1m" << part1(depths)
<< "\033[0m depth increases." << std::endl;
std::cout << "There are \033[91;1m" << part2(depths)
<< "\033[0m window depth increases." << std::endl;
return 0;
}