Skip to content

Instantly share code, notes, and snippets.

@stungeye
Last active December 5, 2020 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stungeye/b92f14390bf6c9b249d5ad0dc5c88e98 to your computer and use it in GitHub Desktop.
Save stungeye/b92f14390bf6c9b249d5ad0dc5c88e98 to your computer and use it in GitHub Desktop.
Advent Of Code Day 1 - C++
#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.
}
}
#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);
}
#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