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