-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.cpp
39 lines (35 loc) · 949 Bytes
/
day01.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
int a(const std::vector<int>& nums, int target = 2020)
{
unordered_set<int> residuals;
for (auto num : nums)
{
if (residuals.count(num))
return (target - num) * num;
residuals.insert(target - num);
}
return -1;
}
int b(const std::vector<int>& nums, int target = 2020)
{
for (size_t i = 0u; i < nums.size(); i++)
{
unordered_map<int, int> residuals;
int new_target = target - nums[i];
for (size_t j = i + 1; j < nums.size(); j++)
{
if (residuals.count(nums[j]))
return residuals[nums[j]] * nums[j];
residuals[new_target - nums[j]] = nums[i] * nums[j];
}
}
return -1;
}
int main()
{
ifstream file("inputs/day01.txt");
vector<int> nums(istream_iterator<int>{file}, istream_iterator<int>{});
cout << a(nums) << "\n"
<< b(nums) << endl;
}