-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_17a.cpp
66 lines (61 loc) · 1.78 KB
/
day_17a.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
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// constexpr int total_eggnog = 25;
constexpr int total_eggnog = 150;
std::vector<bool> convert_decimal_to_binary(const uint64_t n) {
std::vector<bool> ans;
int n_containers = 0;
for (int i = 63; i >= 0; i--) {
uint64_t k = n >> i;
if (k & 1) {
n_containers++;
ans.push_back(true);
} else {
ans.push_back(false);
}
}
return ans;
}
int calc_sum(const std::vector<int>& volumes, const std::vector<bool>& selection) {
int total = 0;
// for (int i = 0; i < selection.size(); i++) {
// std::cout << selection[selection.size() - 1 - i];
// }
for (int i = 0; i < volumes.size(); i++) {
// std::cout << selection[selection.size() - 1 - i];
if (selection[selection.size() - 1 - i]) total += volumes [volumes.size() - 1 - i];
}
// std::cout << '\n';
return total;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_17_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<int> volumes;
while(std::getline(file, line)) {
volumes.push_back(std::stoi(line));
}
int count = 0;
for (uint64_t i = 0; i < std::pow(2, volumes.size()); i++) {
// std::cout << i << ' ';
const auto selection = convert_decimal_to_binary(i);
// for (const auto ele : selection) {
// std::cout << int(ele);
// }
// std::cout << ' ';
const auto sum = calc_sum(volumes, selection);
// std::cout << sum << '\n';
if (sum == total_eggnog) {
count++;
}
}
std::cout << count << '\n';
return 0;
}