-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_03a.cpp
52 lines (45 loc) · 1.04 KB
/
day_03a.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
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
inline int convert_to_binary(const std::vector<int>& bin_vec) {
const int s = bin_vec.size();
int bin_val = 0;
for (int i = 0; i < s; i++) {
bin_val += (bin_vec[s - i - 1]) << i;
}
return bin_val;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_03_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
int n = 0;
std::getline(file, line);
n++;
const size_t s = line.size();
std::vector<int> digits(s);
for (int i = 0; i < line.size(); i++) {
digits[i] = line[i] - '0';
}
while(std::getline(file, line)) {
n++;
for (int i = 0; i < s; i++) {
digits[i] += line[i] - '0';
}
}
for (auto& ele : digits) {
ele = (ele * 2) / n;
}
const int gamma = convert_to_binary(digits);
for (auto& digit : digits) {
digit = std::abs(1 - digit);
}
const int eps = convert_to_binary(digits);
std::cout << gamma * eps << '\n';
return 0;
}