feat: decrease complexity of 2020-12-01 p1 & p2

This commit is contained in:
Rekai Nyangadzayi Musuka 2020-12-01 21:31:03 -06:00
parent 204bab640a
commit df6b6dd888
1 changed files with 43 additions and 33 deletions

View File

@ -1,6 +1,7 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
int part_1();
int part_2();
@ -11,48 +12,57 @@ int main() {
}
int part_1() {
std::vector<int> vector;
std::unordered_map<int, int> map;
std::ifstream input("../input.txt");
std::string line;
int i = 0;
while (std::getline(input, line)) {
int num = std::stoi(line);
vector.push_back(num);
std::pair<int, int> pair {std::stoi(line), i};
map.insert(pair);
i++;
}
for (int i : vector) {
for (int j : vector) {
if (i + j == 2020) {
std::cout << (i * j) << std::endl;
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::vector<int> vector;
std::unordered_map<int, int> map;
std::ifstream input("../input.txt");
std::string line;
int i = 0;
while (std::getline(input, line)) {
int num = std::stoi(line);
vector.push_back(num);
std::pair<int, int> pair {std::stoi(line), i};
map.insert(pair);
i++;
}
for (int i : vector) {
for (int j : vector) {
for (int k : vector) {
if (i + j + k == 2020) {
std::cout << (i * j * k) << std::endl;
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;
}