-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.cpp
147 lines (115 loc) · 3.53 KB
/
day3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <math.h>
using namespace std;
int getPowerConsumption(const string filename, const int binaryLength) {
vector<int> binarySum(binaryLength, 0);
int nNumbers = 0;
ifstream sstream(filename);
for (string ss; getline(sstream, ss); ) {
nNumbers++;
for (int i = 0; i < binaryLength; ++i) {
binarySum[i] += ss[i] - '0';
}
}
for (int i = 0; i < binaryLength; ++i) {
binarySum[i] = binarySum[i] > nNumbers / 2 ? 1 : 0;
}
int gammaRate = 0;
for (int i = 0; i < binaryLength; ++i) {
gammaRate += binarySum[binaryLength-i-1] * pow(2, i);
}
int epsilonRate = pow(2, binaryLength) - 1 - gammaRate;
cout << "gamma = " << gammaRate << ", epsilon = " << epsilonRate << endl;
return gammaRate * epsilonRate;
}
int getLifeSupport(const string filename, const int binaryLength) {
vector<vector<bool>> inputs;
int nNumbers = 0;
ifstream sstream(filename);
for (string ss; getline(sstream, ss); ) {
nNumbers++;
vector<bool> line(binaryLength);
for (int i = 0; i < binaryLength; ++i) {
line[i] = bool((int)ss[i] - '0');
}
inputs.emplace_back(line);
}
vector<bool> valid(nNumbers, true);
auto nValid = [](vector<bool> bVec) {
int n = 0;
for (const bool &b : bVec) {
n += b;
}
return n;
};
for (int i = 0; i < binaryLength; ++i) {
if (nValid(valid) == 1) break;
int bitSum = 0;
for (int j = 0; j < nNumbers; j++) {
if (valid[j]) {
bitSum += inputs[j][i];
}
}
bool bit = bitSum >= float(nValid(valid)) / 2.f ? true : false;
for (int j = 0; j < nNumbers; j++) {
if (valid[j] && inputs[j][i] != bit) {
valid[j] = false;
}
}
}
int validIndex;
for (int i = 0; i < nNumbers; ++i) {
if (valid[i]) {
validIndex = i;
break;
}
}
int oxGenRating = 0;
for (int i = 0; i < binaryLength; ++i) {
cout << inputs[validIndex][i] << ' ';
oxGenRating += inputs[validIndex][binaryLength-i-1] * pow(2, i);
}
cout << endl;
// reset valid list
for (int i = 0; i < nNumbers; ++i) {
valid[i] = true;
}
for (int i = 0; i < binaryLength; ++i) {
if (nValid(valid) == 1) break;
int bitSum = 0;
for (int j = 0; j < nNumbers; j++) {
if (valid[j]) {
bitSum += inputs[j][i];
}
}
bool bit = bitSum >= float(nValid(valid)) / 2.f ? false : true;
for (int j = 0; j < nNumbers; j++) {
if (valid[j] && inputs[j][i] != bit) {
valid[j] = false;
}
}
}
for (int i = 0; i < nNumbers; ++i) {
if (valid[i]) {
validIndex = i;
break;
}
}
int co2ScrubberRating = 0;
for (int i = 0; i < binaryLength; ++i) {
cout << inputs[validIndex][i] << ' ';
co2ScrubberRating += inputs[validIndex][binaryLength-i-1] * pow(2, i);
}
cout << endl;
return oxGenRating * co2ScrubberRating;
}
int main() {
auto powerConsumption = getPowerConsumption("day3.txt", 12);
cout << "Power Consumption = " << powerConsumption << endl;
auto lifeSupport = getLifeSupport("day3.txt", 12);
cout << "Life Support = " << lifeSupport << endl;
}