Reformat day 1

This commit is contained in:
Nick Krichevsky 2020-12-02 01:17:06 -05:00
parent 293c063425
commit ab351305af

View file

@ -2,7 +2,6 @@
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <string> #include <string>
#include <vector>
constexpr int TARGET_NUM = 2020; constexpr int TARGET_NUM = 2020;
@ -12,49 +11,49 @@ constexpr int TARGET_NUM = 2020;
* @return std::set<int> A set of the numbers in the input * @return std::set<int> A set of the numbers in the input
*/ */
std::set<int> read_input(const std::string &filename) { std::set<int> read_input(const std::string &filename) {
std::set<int> input; std::set<int> input;
std::string line; std::string line;
std::ifstream file(filename); std::ifstream file(filename);
while (std::getline(file, line)) { while (std::getline(file, line)) {
int num = std::stoi(line); int num = std::stoi(line);
input.insert(num); input.insert(num);
} }
return input; return input;
} }
int part1(const std::set<int> &inputs) { int part1(const std::set<int> &inputs) {
for (int num : inputs) { for (int num : inputs) {
int desired = TARGET_NUM - num; int desired = TARGET_NUM - num;
if (inputs.find(desired) != inputs.end()) { if (inputs.find(desired) != inputs.end()) {
return desired * num; return desired * num;
} }
} }
throw std::runtime_error("Does not contain solution"); throw std::runtime_error("Does not contain solution");
} }
int part2(const std::set<int> &inputs) { int part2(const std::set<int> &inputs) {
for (int num : inputs) { for (int num : inputs) {
for (int num2 : inputs) { for (int num2 : inputs) {
int desired = TARGET_NUM - (num + num2); int desired = TARGET_NUM - (num + num2);
if (inputs.find(desired) != inputs.end()) { if (inputs.find(desired) != inputs.end()) {
return desired * num2 * num; return desired * num2 * num;
} }
} }
} }
throw std::runtime_error("Does not contain solution"); throw std::runtime_error("Does not contain solution");
} }
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
if (argc != 2) { if (argc != 2) {
std::cerr << "./day1 <input_file>" << std::endl; std::cerr << "./day1 <input_file>" << std::endl;
return 1; return 1;
} }
std::set<int> inputs = read_input(argv[1]); std::set<int> inputs = read_input(argv[1]);
std::cout << part1(inputs) << std::endl; std::cout << part1(inputs) << std::endl;
std::cout << part2(inputs) << std::endl; std::cout << part2(inputs) << std::endl;
} }