-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
78 lines (73 loc) · 2.18 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
73
74
75
76
77
78
/************************************************************************/
/* Advent of Code:
/* Day 24: It Hangs in the Balance
/************************************************************************/
#include <algorithm>
#include <iostream>
#include <chrono>
#include <vector>
using Set = std::vector<int>;
Set input = { 1, 3, 5, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 };
size_t p1_lowest;
uint64_t p1_qe;
int p1_sum;
size_t p2_lowest;
uint64_t p2_qe;
int p2_sum;
void findSum(Set numbers, Set partial)
{
int s = 0;
std::for_each(partial.begin(), partial.end(), [&](const int& element) {s += element; });
if (s == p1_sum)
{
if (partial.size() <= p1_lowest)
{
p1_lowest = partial.size();
uint64_t qe = 1;
std::for_each(partial.begin(), partial.end(), [&](const int& element) {qe *= element; });
p1_qe = std::min(p1_qe, qe);
}
}
if (s == p2_sum)
{
if (partial.size() <= p2_lowest)
{
p2_lowest = partial.size();
uint64_t qe = 1;
std::for_each(partial.begin(), partial.end(), [&](const int& element) {qe *= element; });
p2_qe = std::min(p2_qe, qe);
}
}
if (s >= p1_sum && s >= p2_sum)
return;
int n;
for (Set::const_iterator ai = numbers.begin(); ai != numbers.end(); ai++)
{
n = *ai;
Set remaining;
for (Set::const_iterator aj = ai; aj != numbers.end(); aj++)
{
if (aj == ai)continue;
remaining.push_back(*aj);
}
Set partial_rec = partial;
partial_rec.push_back(n);
findSum(remaining, partial_rec);
}
}
int main()
{
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
int sum = 0;
p1_qe = p2_qe = 1;
p1_lowest = p2_lowest = input.size();
std::for_each(input.begin(), input.end(), [&sum](const int& element) {sum += element; p1_qe = p2_qe = p1_qe*element; });
p1_sum = sum / 3;
p2_sum = sum / 4;
findSum(input, Set());
std::cout << "Part One: " << p1_qe << std::endl;
std::cout << "Part Two: " << p2_qe << 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.";
return 1;
}