Guesses are cleaned from information already known
This commit is contained in:
parent
7aae78f92e
commit
de5b6f183a
1 changed files with 46 additions and 7 deletions
53
solver.hpp
53
solver.hpp
|
@ -78,12 +78,40 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
// Clear what we already know
|
||||
Historic_guess clean(Historic_guess hist) {
|
||||
// The correct colors we know
|
||||
for(int n = 0; n < known.N; n++) {
|
||||
if(known.final_color(n) == hist.guess[n]) {
|
||||
hist.guess[n] = -1;
|
||||
hist.response[1] -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
// The out-of-place colors we know
|
||||
for(int n = 0; n < known.N; n++) {
|
||||
if(hist.guess[n] == -1)
|
||||
continue;
|
||||
for(int i = 0; i < known.N; i++) {
|
||||
if(i == n)
|
||||
continue;
|
||||
if(known.final_color(i) == hist.guess[n]) {
|
||||
hist.guess[n] = -1;
|
||||
hist.response[0] -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hist;
|
||||
}
|
||||
|
||||
// Specific reactions
|
||||
void if_not_here_then_nowhere(vector<int> guess) {
|
||||
// Get all positions of these colors
|
||||
auto positions_of_colors = vector<vector<int>>(known.M, vector<int>(0));
|
||||
for(int n = 0; n < known.N; n++)
|
||||
positions_of_colors[guess[n]].push_back(n);
|
||||
if(guess[n] > -1)
|
||||
positions_of_colors[guess[n]].push_back(n);
|
||||
|
||||
// If color can't be anywhere here, it can't be in the sequence
|
||||
for(int col = 0; col < known.M; col++) {
|
||||
|
@ -97,20 +125,31 @@ public:
|
|||
}
|
||||
void not_here(vector<int> guess) {
|
||||
for(int n = 0; n < known.N; n++)
|
||||
known.cannot_be(n, guess[n]);
|
||||
if(guess[n] > -1)
|
||||
known.cannot_be(n, guess[n]);
|
||||
}
|
||||
void empty(vector<int> guess) {
|
||||
for(int col : guess)
|
||||
known.empty_color(col);
|
||||
if(col > -1)
|
||||
known.empty_color(col);
|
||||
}
|
||||
|
||||
bool learn_back(Historic_guess hist) {
|
||||
bool something_to_learn = true;
|
||||
// TODO
|
||||
|
||||
hist = clean(hist);
|
||||
// TODO Actually learn
|
||||
|
||||
return something_to_learn;
|
||||
}
|
||||
void learn(vector<int> guess, vector<int> response) {
|
||||
void learn(vector<int> p_guess, vector<int> p_response) {
|
||||
bool something_to_learn;
|
||||
|
||||
// A bit of cleaning
|
||||
auto cleaned = clean({p_guess, p_response});
|
||||
vector<int> guess = cleaned.guess;
|
||||
vector<int> response = cleaned.response;
|
||||
|
||||
// None of these colors are there
|
||||
if(response[0] == 0 && response[1] == 0) {
|
||||
empty(guess);
|
||||
|
@ -126,6 +165,7 @@ public:
|
|||
// At least only on the right spot
|
||||
else if(response[0] == 0) {
|
||||
if_not_here_then_nowhere(guess);
|
||||
// TODO maybe I can guess which one is right
|
||||
something_to_learn = true;
|
||||
}
|
||||
|
||||
|
@ -142,10 +182,9 @@ public:
|
|||
i--;
|
||||
}
|
||||
|
||||
|
||||
// Write to history
|
||||
if(something_to_learn)
|
||||
history.push_back({guess, response});
|
||||
history.push_back({p_guess, p_response});
|
||||
}
|
||||
void print() {
|
||||
known.print();
|
||||
|
|
Loading…
Reference in a new issue