diff options
Diffstat (limited to '2020/src/day01.cpp')
| -rw-r--r-- | 2020/src/day01.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/2020/src/day01.cpp b/2020/src/day01.cpp new file mode 100644 index 0000000..d2d8629 --- /dev/null +++ b/2020/src/day01.cpp | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <functional> | ||
| 3 | #include <iostream> | ||
| 4 | #include <string> | ||
| 5 | #include <vector> | ||
| 6 | #include <numeric> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | auto | ||
| 10 | read_file(string_view filename) | ||
| 11 | { | ||
| 12 | fstream input{ filename }; | ||
| 13 | vector<long> data; | ||
| 14 | |||
| 15 | for ( long value = 0; input >> value; ) { | ||
| 16 | data.emplace_back(value); | ||
| 17 | } | ||
| 18 | |||
| 19 | return data; | ||
| 20 | } | ||
| 21 | |||
| 22 | void | ||
| 23 | process(const vector<long>& values) | ||
| 24 | { | ||
| 25 | const long year = 2020; | ||
| 26 | |||
| 27 | if (accumulate(values.begin(), values.end(), 0L) == year) { | ||
| 28 | cout << accumulate(values.begin(), values.end(), 1, multiplies<>()) << endl; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | void | ||
| 33 | combination(const vector<long>& values, size_t r) | ||
| 34 | { | ||
| 35 | vector<bool> v(values.size()); | ||
| 36 | fill(v.end() - long(r), v.end(), true); | ||
| 37 | vector<long> set(r); | ||
| 38 | do { | ||
| 39 | set.clear(); | ||
| 40 | for ( size_t i = 0; i != values.size(); ++i ) { | ||
| 41 | if ( v[i] ) { | ||
| 42 | set.emplace_back(values[i]); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | process(set); | ||
| 46 | } while ( std::next_permutation(v.begin(), v.end()) ); | ||
| 47 | } | ||
| 48 | |||
| 49 | int | ||
| 50 | main() | ||
| 51 | { | ||
| 52 | auto values = read_file("data/day01-sample1.txt"); | ||
| 53 | combination(values, 2); | ||
| 54 | combination(values, 3); | ||
| 55 | } | ||
