Advent-of-Code/2020/1/main.cpp

44 lines
943 B
C++

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int find2(std::vector<int> &numbers)
{
for(auto i = 0; i < numbers.size(); ++i)
for(auto j = i + 1; j < numbers.size(); ++j)
if(numbers[i] + numbers[j] == 2020)
return numbers[i] * numbers[j];
return 0;
}
int find3(std::vector<int> &numbers)
{
for(auto i = 0; i < numbers.size(); ++i)
for(auto j = i + 1; j < numbers.size(); ++j)
for(auto k = i + 1; k < numbers.size(); ++k)
if(numbers[i] + numbers[j] + numbers[k] == 2020)
return numbers[i] * numbers[j] * numbers[k];
return 0;
}
int main()
{
std::ifstream ifs("data");
if(!ifs.is_open())
{
std::cerr << "Couldn't open file." << std::endl;
return -1;
}
std::vector<int> numbers;
for(std::string line; std::getline(ifs, line); )
numbers.push_back(std::atoi(line.c_str()));
std::cout << find2(numbers) << std::endl;
std::cout << find3(numbers) << std::endl;
return 0;
}