-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_17b.cpp
61 lines (57 loc) · 1.74 KB
/
day_17b.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
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// constexpr int total_eggnog = 25;
constexpr int total_eggnog = 150;
std::pair<std::vector<bool>, int> 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 {std::move(ans), n_containers};
}
int calc_sum(const std::vector<int>& volumes, const std::vector<bool>& selection) {
int total = 0;
for (int i = 0; i < volumes.size(); i++) {
if (selection[selection.size() - 1 - i]) total += volumes [volumes.size() - 1 - i];
}
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;
int n_containers = volumes.size();
for (uint64_t i = 0; i < std::pow(2, volumes.size()); i++) {
const auto [selection, new_n_containers] = convert_decimal_to_binary(i);
if (new_n_containers > n_containers) continue;
const auto sum = calc_sum(volumes, selection);
if (sum == total_eggnog) {
if (n_containers > new_n_containers) {
n_containers = new_n_containers;
count = 1;
} else if (n_containers == new_n_containers) {
count++;
}
}
}
std::cout << count << '\n';
return 0;
}