Skip to content

Latest commit

 

History

History
147 lines (106 loc) · 3.32 KB

day01.org

File metadata and controls

147 lines (106 loc) · 3.32 KB

Day 1: Report Repair

https://adventofcode.com/2020/day/1

Part 1

Problem

Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn’t quite adding up.

Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together.

For example, suppose your expense report contained the following:

1721
979
366
299
675
1456

In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579.

Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together?

Solution

#include <iostream>
#include <fstream>
#include <unordered_map>
using namespace std;

int twoSumProduct(const string& filename, int sum) {
    unordered_map<int, bool> seen;
    ifstream file(filename);
    int n;

    while(file) {
        file >> n;

        if (seen[sum - n]) {
            return n * (sum - n);
        } else {
            seen[n] = true;
        }
    }

    file.close();
}

int main() {
    cout << twoSumProduct("input", 2020);
}

Output

32064

Part 2

Problem

The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria.

Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. Multiplying them together produces the answer, 241861950.

In your expense report, what is the product of the three entries that sum to 2020?

Solution

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

void displayVector(const vector<int>& a) {
    for (auto i : a) {
        cout << i << " ";
    }

    cout << "\n";
}

void threeSumProduct(vector<int>& list, int sum) {
    sort(list.begin(), list.end());

    int n = list.size();

    for (int i = 0; i < n-2; ++i) {
        int left = i + 1;
        int right = n - 1;

        while (left < right) {
            int tripletSum = list[i] + list[left] + list[right];

            if (tripletSum == sum) {
                cout << (list[i] * list[left] * list[right]) << "\n";
                return;
            } else if (tripletSum < sum) {
                ++left;
            } else {
                --right;
            }
        }
    }

}

vector<int> listFromFile(const string& filename) {
    vector<int> list;
    ifstream file(filename);
    int n;

    while (file) {
        file >> n;
        list.push_back(n);
    }

    file.close();
    return list;
}

int main() {
    vector<int> list = listFromFile("input");
    threeSumProduct(list, 2020);
}

Output

193598720