Fixed cleaning and if_not_here_then_nowhere
This commit is contained in:
parent
8b5a69ffbc
commit
ea6f60fc29
2 changed files with 34 additions and 13 deletions
46
solver.cpp
46
solver.cpp
|
@ -31,6 +31,12 @@ int Game::final_color(int n) {
|
||||||
return final_col;
|
return final_col;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
bool Game::is_empty(int col) {
|
||||||
|
for(int n = 0; n < N; n++)
|
||||||
|
if(possible[n][col])
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
vector<vector<int>> Game::list_all_possibilities() {
|
vector<vector<int>> Game::list_all_possibilities() {
|
||||||
auto r = vector<vector<int>>(N, vector<int>(0));
|
auto r = vector<vector<int>>(N, vector<int>(0));
|
||||||
for(int col = 0; col < M; col++)
|
for(int col = 0; col < M; col++)
|
||||||
|
@ -102,7 +108,7 @@ void Game::all_are_here(vector<int> guess) {
|
||||||
auto positions_of_colors = get_positions_of_colors(guess);
|
auto positions_of_colors = get_positions_of_colors(guess);
|
||||||
|
|
||||||
for(int col = 0; col < M; col++) {
|
for(int col = 0; col < M; col++) {
|
||||||
if(!positions_of_colors[col].size())
|
if(positions_of_colors[col].size() == 0)
|
||||||
empty_color(col);
|
empty_color(col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,8 +120,8 @@ Historic_guess::Historic_guess(vector<int> p_guess, vector<int> p_response) {
|
||||||
response = p_response;
|
response = p_response;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Solver
|
|
||||||
|
|
||||||
|
// Solver
|
||||||
Solver::Solver(int p_N, int p_M) : N(p_N), M(p_M), known({p_N, p_M}) {}
|
Solver::Solver(int p_N, int p_M) : N(p_N), M(p_M), known({p_N, p_M}) {}
|
||||||
|
|
||||||
// Check, if it could have been this
|
// Check, if it could have been this
|
||||||
|
@ -160,26 +166,35 @@ void Solver::print() {
|
||||||
|
|
||||||
// Clean guess and response from info we know
|
// Clean guess and response from info we know
|
||||||
Historic_guess Solver::clean(Historic_guess hist) {
|
Historic_guess Solver::clean(Historic_guess hist) {
|
||||||
|
vector<bool> already_used_in_cleaning(N, 0);
|
||||||
|
|
||||||
|
// Clean empty colors
|
||||||
|
for(int n = 0; n < N; n++)
|
||||||
|
if(known.is_empty(hist.guess[n]))
|
||||||
|
hist.guess[n] = -2;
|
||||||
|
|
||||||
// The in-place colors we know
|
// The in-place colors we know
|
||||||
for(int n = 0; n < N; n++) {
|
for(int n = 0; n < N; n++) {
|
||||||
if(hist.guess[n] == -1)
|
if(hist.guess[n] <= -1)
|
||||||
continue;
|
continue;
|
||||||
if(known.final_color(n) == hist.guess[n]) {
|
if(known.final_color(n) == hist.guess[n]) {
|
||||||
hist.guess[n] = -1;
|
hist.guess[n] = -1;
|
||||||
hist.response[1] -= 1;
|
hist.response[1] -= 1;
|
||||||
|
already_used_in_cleaning[n] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The out-of-place colors we know
|
// The out-of-place colors we know
|
||||||
for(int n = 0; n < N; n++) {
|
for(int n = 0; n < N; n++) {
|
||||||
if(hist.guess[n] == -1)
|
if(hist.guess[n] <= -1 || known.can(n, hist.guess[n]))
|
||||||
continue;
|
continue;
|
||||||
for(int i = 0; i < N; i++) {
|
for(int i = 0; i < N; i++) {
|
||||||
if(i == n || hist.guess[i] == -1)
|
if(i == n || hist.guess[i] <= -1 || already_used_in_cleaning[i])
|
||||||
continue;
|
continue;
|
||||||
if(known.final_color(i) == hist.guess[n]) {
|
if(known.final_color(i) == hist.guess[n]) {
|
||||||
hist.guess[n] = -1;
|
hist.guess[n] = -1;
|
||||||
hist.response[0] -= 1;
|
hist.response[0] -= 1;
|
||||||
|
already_used_in_cleaning[i] = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,8 +220,8 @@ bool Solver::extract_info(Historic_guess hist) {
|
||||||
possible_count++;
|
possible_count++;
|
||||||
|
|
||||||
// None of these colors are there
|
// None of these colors are there
|
||||||
if(response[0] == 0 && response[1] == 0) {
|
if(hist.response[0] == 0 && hist.response[1] == 0) {
|
||||||
known.empty(guess);
|
known.empty(hist.guess);
|
||||||
something_to_learn = false;
|
something_to_learn = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,8 +236,10 @@ bool Solver::extract_info(Historic_guess hist) {
|
||||||
known.here(guess);
|
known.here(guess);
|
||||||
something_to_learn = false;
|
something_to_learn = false;
|
||||||
}
|
}
|
||||||
else
|
// TODO tady
|
||||||
known.if_not_here_then_nowhere(guess);
|
else if(hist.response[0] == 0){
|
||||||
|
known.if_not_here_then_nowhere(hist.guess);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nonzero / nonzero
|
// Nonzero / nonzero
|
||||||
|
@ -232,13 +249,16 @@ bool Solver::extract_info(Historic_guess hist) {
|
||||||
known.here(guess);
|
known.here(guess);
|
||||||
}
|
}
|
||||||
|
|
||||||
// All guessed colors are in the sequence
|
|
||||||
if(response[0] + response[1] == N)
|
|
||||||
known.all_are_here(guess);
|
|
||||||
|
|
||||||
return something_to_learn;
|
return something_to_learn;
|
||||||
}
|
}
|
||||||
void Solver::learn(vector<int> p_guess, vector<int> p_response) {
|
void Solver::learn(vector<int> p_guess, vector<int> p_response) {
|
||||||
|
// All guessed colors are in the sequence
|
||||||
|
if(p_response[0] + p_response[1] == N)
|
||||||
|
known.all_are_here(p_guess);
|
||||||
|
|
||||||
|
if(p_guess[0] == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
// Write to history
|
// Write to history
|
||||||
history.push_back({p_guess, p_response});
|
history.push_back({p_guess, p_response});
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ public:
|
||||||
// Get known information
|
// Get known information
|
||||||
bool can(int n, int col);
|
bool can(int n, int col);
|
||||||
int final_color(int n);
|
int final_color(int n);
|
||||||
|
bool is_empty(int col);
|
||||||
void print();
|
void print();
|
||||||
vector<vector<int>> list_all_possibilities();
|
vector<vector<int>> list_all_possibilities();
|
||||||
vector<vector<int>> get_positions_of_colors(vector<int> guess);
|
vector<vector<int>> get_positions_of_colors(vector<int> guess);
|
||||||
|
|
Loading…
Reference in a new issue