Problem Description
Hello, I'm re-writing an old program I made that solves nonograms. As part of solving it, I generate (for some of the lines) an array of all possible ways to fit the numbers in.
I want to create a piece of code, that will merge all the possible placements.
Meaning, that for example, in pseudo code:
[[0, 0, 1],
[1, 0, 1],
[0, 1, 1]]
OR - [1, 1, 1]
AND - [0, 0, 1]
The result of the two operations will let me know exactly where we have a fixed value.
[?, ?, 1]
Assumptions
The solution is desired in C++.
The placements are stored in a
vector<vector<bool>>
.The size of all the inner
vector
s is the same.It doesn't matter which bitwise operation is done, just that it can be applied to all the vectors.
Possible Solution
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<vector<bool>> options =
{{false, false, true},
{true, false, true},
{false, true, true}};
vector<bool> merge_and, merge_or;
bool flag;
int i, j;
for (i = 0; i < options.size(); i++) {
for (j = 0; j < options[0].size(); j++) cout << options[i][j];
cout << endl;
}
cout << endl;
for (i = 0; i < options[0].size(); i++) {
flag = options[0][i];
for (j = 1; j < options.size(); j++) {
flag = flag & options[j][i];
}
merge_and.push_back(flag);
}
for (i = 0; i < options[0].size(); i++) {
flag = options[0][i];
for (j = 1; j < options.size(); j++) {
flag = flag | options[j][i];
}
merge_or.push_back(flag);
}
cout << "AND ";
for (const auto &i : merge_and) cout << i;
cout << endl;
cout << "OR ";
for (const auto &i : merge_or) cout << i;
cout << endl;
}
Output:
001
101
011
AND 001
OR 001
Further Metadata
If anyone is interested in the full implementation, once it's done and fully documented, I will upload it to my GitHub account.
Thanks in advance for the helpers and commentators.