Thanks for wasting an hour of my life, bastards.
#include <iostream>#include <vector>#include <algorithm>using namespace std;const int NUM_COLORS = 4;typedef int Color;struct Arrangement{ Arrangement(Color color0, Color color1, Color color2, Color color3) { colors[0] = color0; colors[1] = color1; colors[2] = color2; colors[3] = color3; } Color colors[4];};typedef int Result;const int NUM_RESULTS = 25; // actually much lessResult MakeResult(int placed, int misplaced){ return placed*5 + misplaced;}Result MatchUp(const Arrangement &arr1, const Arrangement &arr2){ int placed = 0, misplaced = 0; bool used1[4] = { false, false, false, false }; bool used2[4] = { false, false, false, false }; // first get placed for(int i=0; i<4; i++) { if(arr1.colors[i] == arr2.colors[i]) { used1[i] = true; used2[i] = true; placed++; } } // now get misplaced for(int i=0; i<4; i++) { if(used1[i]) { continue; } for(int j=0; j<4; j++) { if(used2[j]) { continue; } if(arr1.colors[i] == arr2.colors[j]) { used1[i] = true; used2[j] = true; misplaced++; } } } return MakeResult(placed, misplaced);}int _tmain(int argc, _TCHAR* argv[]){ vector<Arrangement> possibilities; for(int i0=0; i0<NUM_COLORS; i0++) for(int i1=0; i1<NUM_COLORS; i1++) for(int i2=0; i2<NUM_COLORS; i2++) for(int i3=0; i3<NUM_COLORS; i3++) possibilities.push_back(Arrangement(i0, i1, i2, i3)); vector<Arrangement> guessPossibilities = possibilities; while(true) { int biggestGrouping = INT_MAX; vector<Arrangement>::const_iterator best = guessPossibilities.end(); for(vector<Arrangement>::const_iterator ig = guessPossibilities.begin(); ig != guessPossibilities.end(); ++ig) { vector<int> resultCounts(NUM_RESULTS, 0); for(vector<Arrangement>::const_iterator ip = possibilities.begin(); ip != possibilities.end(); ++ip) { resultCounts[MatchUp(*ig, *ip)]++; } int guessBiggestGrouping = * max_element(resultCounts.begin(), resultCounts.end()); if(guessBiggestGrouping < biggestGrouping) { biggestGrouping = guessBiggestGrouping; best = ig; } } cout << "I''m guessing " << best->colors[0] << ", " << best->colors[1] << ", " << best->colors[2] << ", " << best->colors[3] << ".\n" << "Number placed correctly? "; int placed, misplaced; cin >> placed; cout << "Number placed incorrectly? "; cin >> misplaced; Result result = MakeResult(placed, misplaced); for(vector<Arrangement>::iterator ip = possibilities.begin(); ip != possibilities.end();) { if(result != MatchUp(*best, *ip)) { ip = possibilities.erase(ip); } else { ++ip; } } if(possibilities.size() == 0) { cout << "YUO HAVE MESED UP! NO CAEK!\n"; exit(1); } if(possibilities.size() == 1) { cout << "Ooh! I know! I know!\n" << possibilities.begin()->colors[0] << ", " << possibilities.begin()->colors[1] << ", " << possibilities.begin()->colors[2] << ", " << possibilities.begin()->colors[3] << "!\n"; exit(0); } }}
How appropriate. You fight like a cow.