Last active
December 5, 2020 04:05
-
-
Save stungeye/b92f14390bf6c9b249d5ad0dc5c88e98 to your computer and use it in GitHub Desktop.
Advent Of Code Day 1 - C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ExpenseReport.h" | |
#include <fstream> | |
#include <iostream> | |
namespace ExpenseReport { | |
// Reads each line of the input file in a set of unique integers. | |
// Note: Testing a set to see if it contains specific numbers is a very quick operation using find(). | |
std::unordered_set<int> read_expense_report_file(const std::string& filename) { | |
std::ifstream input_file{filename}; | |
if (!input_file) { | |
std::cerr << "Cannot open the File : " << filename << "\n"; | |
} | |
std::unordered_set<int> expense_report{std::istream_iterator<int>{input_file}, {}}; | |
return expense_report; | |
} | |
// Searches through a set of unique numbers for two entries that add up to a given sum. | |
// Returns the products of these two numbers. | |
int product_of_two_expenses_that_equal_sum(const std::unordered_set<int>& expenses, const int& sum) { | |
// Test each number in the set to see... | |
for (const auto& candidate1 : expenses) { | |
// ...if there's another number in the set that can be added to the first to make a given sum. | |
const auto candidate2 = sum - candidate1; | |
if (expenses.find(candidate2) != expenses.end()) { | |
return candidate1 * candidate2; | |
} | |
} | |
return 0; // If no pair was found. | |
} | |
// Searches through a set of unique numbers for three entries that add up to a given sum. | |
// Returns the products of these three numbers. | |
int product_of_three_expenses_that_equal_sum(const std::unordered_set<int>& expenses, const int& sum) { | |
// Nest for-loops to test all possible combinations of pairs in the set to see... | |
for (const auto& candidate1 : expenses) { | |
for (const auto& candidate2 : expenses) { | |
// ...if there's a third number in the set that when added to the pair makes a given sum. | |
const auto candidate3 = sum - candidate1 - candidate2; | |
if (expenses.find(candidate3) != expenses.end()) { | |
return candidate1 * candidate2 * candidate3; | |
} | |
} | |
} | |
return 0; // If no triplet was found. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
#include <unordered_set> | |
namespace ExpenseReport { | |
std::unordered_set<int> read_expense_report_file(const std::string& filename); | |
int product_of_two_expenses_that_equal_sum(const std::unordered_set<int>& expenses, const int& sum); | |
int product_of_three_expenses_that_equal_sum(const std::unordered_set<int>& expenses, const int& sum); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ExpenseReport.h" | |
#include "test/catch.hpp" // The Catch2 testing library. | |
#include <unordered_set> | |
TEST_CASE("File Input Test") { | |
const std::string filename = "ExpenseReport.txt"; | |
REQUIRE(ExpenseReport::read_expense_report_file(filename).size() == 200); | |
} | |
TEST_CASE("Mini Input Test - Two Numbers") { | |
const std::unordered_set<int> expenses{ 1721, 979, 366, 299, 675, 1456 }; | |
REQUIRE(ExpenseReport::product_of_two_expenses_that_equal_sum(expenses, 2020) == 514579); | |
} | |
TEST_CASE("Full Input Test - Two Numbers") { | |
const std::string filename = "ExpenseReport.txt"; | |
const std::unordered_set<int> expenses = ExpenseReport::read_expense_report_file(filename); | |
REQUIRE(ExpenseReport::product_of_two_expenses_that_equal_sum(expenses, 2020) == 436404); | |
} | |
TEST_CASE("Mini Input Test - Three Numbers") { | |
const std::unordered_set<int> expenses{ 1721, 979, 366, 299, 675, 1456 }; | |
REQUIRE(ExpenseReport::product_of_three_expenses_that_equal_sum(expenses, 2020) == 241861950); | |
} | |
TEST_CASE("Full Input Test - Three Numbers") { | |
const std::string filename = "ExpenseReport.txt"; | |
const std::unordered_set<int> expenses = ExpenseReport::read_expense_report_file(filename); | |
REQUIRE(ExpenseReport::product_of_three_expenses_that_equal_sum(expenses, 2020) == 274879808); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment