aboutsummaryrefslogtreecommitdiff
path: root/2020/src/day01.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-01-03 23:35:54 +0100
committerThomas Schmucker <ts@its1.de>2024-01-03 23:35:54 +0100
commit56e890cec0a28c0a485212ccebfaf774235a79a2 (patch)
treed9c6241a1aa247e06ab5ba2f6f11967b77d458ec /2020/src/day01.cpp
parente9a1cb0441d137d7a26cb303b7e72d3424b7392d (diff)
downloadadvent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.gz
advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.bz2
advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.zip
prepare for more puzzles ... :)
Diffstat (limited to '2020/src/day01.cpp')
-rw-r--r--2020/src/day01.cpp55
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>
7using namespace std;
8
9auto
10read_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
22void
23process(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
32void
33combination(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
49int
50main()
51{
52 auto values = read_file("data/day01-sample1.txt");
53 combination(values, 2);
54 combination(values, 3);
55}