From 24a4d456409a66a2ca92d5d629dfa3ff34c19ec9 Mon Sep 17 00:00:00 2001 From: Matuush Date: Sun, 3 Nov 2024 11:24:17 +0100 Subject: [PATCH] If response only contains right positions, the colors still can be somewhere else --- solver.hpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/solver.hpp b/solver.hpp index c6e2a44..42d9e69 100644 --- a/solver.hpp +++ b/solver.hpp @@ -53,21 +53,35 @@ public: // TODO return {}; } - void learn_back(vector guess) { + void learn_back(vector historic_guess) { // TODO } void learn(vector guess, vector response) { // Reduce color positions - if(response[0] == 0 && response[1] == 0) { + if(response[0] == 0 && response[1] == 0) { // None of these colors are there for(int col : guess) known.empty_color(col); } - else if(response[1] == 0) {; + else if(response[1] == 0) { // None at the right spot + // Not here for(int n = 0; n < known.N; n++) known.cannot_be(n, guess[n]); } - else if(response[0] == 0) { - // TODO if somewhere, then at least here + else if(response[0] == 0) { // At least only on the right spot + // Get all positions of these colors + auto positions_of_colors = vector>(known.M, vector(0)); + for(int n = 0; n < known.N; n++) + 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++) { + int possible_count = 0; + for(int n : positions_of_colors[col]) + if(known.possible[n][col]) + possible_count++; + if(possible_count == 0 && positions_of_colors[col].size() > 0) + known.empty_color(col); + } } else { // TODO nonzero / nonzero @@ -77,6 +91,9 @@ public: for(vector past_guess : history) learn_back(past_guess); + // Write to history + for(int r : response) + guess.push_back(r); history.push_back(guess); } void print() {