blob: d2d8629f18ac119f1ce68a78db48a6bb311de979 (
plain)
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
|
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
using namespace std;
auto
read_file(string_view filename)
{
fstream input{ filename };
vector<long> data;
for ( long value = 0; input >> value; ) {
data.emplace_back(value);
}
return data;
}
void
process(const vector<long>& values)
{
const long year = 2020;
if (accumulate(values.begin(), values.end(), 0L) == year) {
cout << accumulate(values.begin(), values.end(), 1, multiplies<>()) << endl;
}
}
void
combination(const vector<long>& values, size_t r)
{
vector<bool> v(values.size());
fill(v.end() - long(r), v.end(), true);
vector<long> set(r);
do {
set.clear();
for ( size_t i = 0; i != values.size(); ++i ) {
if ( v[i] ) {
set.emplace_back(values[i]);
}
}
process(set);
} while ( std::next_permutation(v.begin(), v.end()) );
}
int
main()
{
auto values = read_file("data/day01-sample1.txt");
combination(values, 2);
combination(values, 3);
}
|