aboutsummaryrefslogtreecommitdiff
path: root/2020/src/day01.cpp
blob: 67f88c46676da3cb38f931233f05d7f469d8d682 (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
56
57
58
#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> marker(values.size());

	fill(marker.end() - long(r), marker.end(), true);

	vector<long> subset(r);

	do {
		subset.clear();
		for ( size_t i = 0; i != values.size(); ++i ) {
			if ( marker[i] ) {
				subset.emplace_back(values[i]);
			}
		}
		process(subset);
	} while ( std::next_permutation(marker.begin(), marker.end()) );
}

int
main()
{
	auto values = read_file("data/day01.txt");
	combination(values, 2);
	combination(values, 3);
}