-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (64 loc) · 1.6 KB
/
main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/************************************************************************/
/* Advent of Code:
/* Day 20: Infinite Elves and Infinite Houses
/************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
#include <chrono>
#include <map>
std::vector<int64_t> findAllDivisors(int64_t n)
{
std::vector<int64_t> divisors = { 1,n };
int64_t end = static_cast<int64_t>(sqrt(n));
for (int64_t i = 2; i <= end; i++)
{
if (n % i == 0)
{
divisors.push_back(i);
if (end != i)
divisors.push_back(n / i);
}
}
return divisors;
}
int64_t partOne()
{
int64_t x = 1;
for (;;)
{
int64_t sum = 0;
x++;
std::vector<int64_t> divisors = findAllDivisors(x);
std::for_each(divisors.begin(), divisors.end(), [&](const int64_t& elem) {sum += elem * 10; });
if (sum >= 33100000)
break;
}
return x;
}
int64_t partTwo()
{
int64_t x = 0;
for (;;)
{
int64_t sum = 0;
x++;
std::vector<int64_t> divisors = findAllDivisors(x);
std::for_each(divisors.begin(), divisors.end(), [&](const int64_t& elem) {\
if (elem*50 >= x)\
sum += (elem*11);\
});
if (sum >= 33100000)
break;
}
return x;
}
int main()
{
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
std::cout << "Part One: " << partOne() << std::endl;
std::cout << "Part Two: " << partTwo() << std::endl;
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - t1);
std::cout << "Time: " << time_span.count() << "s.";
}