This repository has been archived on 2021-06-30. You can view files and clone it, but cannot push or open issues or pull requests.
aoc2020/day_1/main.cpp

70 lines
1.5 KiB
C++

#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
int part_1();
int part_2();
int main() {
part_1();
part_2();
}
int part_1() {
std::unordered_map<int, int> map;
std::ifstream input("../input.txt");
std::string line;
int i = 0;
while (std::getline(input, line)) {
std::pair<int, int> pair {std::stoi(line), i};
map.insert(pair);
i++;
}
for (std::pair<int, int> pair : map) {
int num = pair.first;
int compliment = 2020 - num;
if (map.contains(compliment)) {
std::cout << (num * compliment) << std::endl;
return 0;
}
}
return 1;
}
int part_2() {
std::unordered_map<int, int> map;
std::ifstream input("../input.txt");
std::string line;
int i = 0;
while (std::getline(input, line)) {
std::pair<int, int> pair {std::stoi(line), i};
map.insert(pair);
i++;
}
for (std::pair<int, int> first_pair : map) {
int num = first_pair.first;
int compliment = 2020 - num;
// Still two other numbers we gotta find from here
for (std::pair<int, int> second_pair : map) {
int second_num = second_pair.first;
int second_compliment = compliment - second_num;
if (map.contains(second_compliment)) {
std::cout << (num * second_num * second_compliment) << std::endl;
return 0;
}
}
}
return 1;
}