solvemind.c
Response calcResponse(Code input, Code hidden) {
    /*
     * This is actually rather complicated because of the
     * possibility of multiple occurences of the same color in the codes.
     *
     * This code uses the idea that the sum of 'fit' and 'misplaced' is equal
     * to the cardinality of the multiset-intersection of the codes viewed as multisets
     * by disregarding the order of the colors.
     *
     * Calculating 'fit' is easy. 'misplaced' is the difference of that
     * cardinality and 'fit'.
     */

    Response result;
    int countColorsInput[COLOR_COUNT];
    int countColorsHidden[COLOR_COUNT];

    //set to 0
    for(int i = 0; i < COLOR_COUNT; i++) {
        countColorsInput[i] = 0;
        countColorsHidden[i] = 0;
    }

    //count colors
    for(int i = 0; i < CODE_LENGTH; i++) {
        countColorsInput[(size_t) input.colors[i]]++;
        countColorsHidden[(size_t) hidden.colors[i]]++;
    }

    //find how many matching colors are in input
    int matchingColors = 0;
    for(int i = 0; i < COLOR_COUNT; i++) {
        int countInput = countColorsInput[i];
        int countHidden = countColorsHidden[i];
        //maximum of matching colors is hidden's color count
        matchingColors += (countInput < countHidden) ? countInput : countHidden;
    }

    //find "fit"
    int fit = 0;
    for(int i = 0; i < CODE_LENGTH; i++) {
        if(input.colors[i] == hidden.colors[i]) {
            fit++;
        }
    }

    result.fit = fit;
    //misplaced is matchingColors which are not "fit"
    result.misplaced = matchingColors - fit;

    return result;
}
/*
 * The heart of the game. The player gets kind of distance measure
 * between his input and the secret code as response.
 *
 * One number shows how many color inputs are in the right spot (result.fit,
 * usually red response pins). Its the complement of the Hamming distance. The
 * other shows how many colors would be correct IF they were in different spot
 * (result.misplaced, white response pins).
 */
The heart of the game. The player gets kind of distance measure
between his input and the secret code as response.

One number shows how many color inputs are in the right spot (result.fit,
usually red response pins). Its the complement of the Hamming distance. The
other shows how many colors would be correct IF they were in different spot
(result.misplaced, white response pins).
The heart of the game . The player gets kind of distance measure between his input and the secret code as response . One number shows how many color inputs are in the right spot ( result . fit usually red response pins ) . Its the complement of the Hamming distance . The other shows how many colors would be correct IF they were in different spot ( result . misplaced white response pins ) .
Response calcResponse(Code input, Code hidden) {
    Response result;
    int countColorsInput[COLOR_COUNT];
    int countColorsHidden[COLOR_COUNT];
    for(int i = 0; i < COLOR_COUNT; i++) {
        countColorsInput[i] = 0;
        countColorsHidden[i] = 0;
    }
    for(int i = 0; i < CODE_LENGTH; i++) {
        countColorsInput[(size_t) input.colors[i]]++;
        countColorsHidden[(size_t) hidden.colors[i]]++;
    }
    int matchingColors = 0;
    for(int i = 0; i < COLOR_COUNT; i++) {
        int countInput = countColorsInput[i];
        int countHidden = countColorsHidden[i];
        matchingColors += (countInput < countHidden) ? countInput : countHidden;
    }
    int fit = 0;
    for(int i = 0; i < CODE_LENGTH; i++) {
        if(input.colors[i] == hidden.colors[i]) {
            fit++;
        }
    }
    result.fit = fit;
    result.misplaced = matchingColors - fit;
    return result;
}
Response calcResponse ( Code input , Code hidden ) { Response result ; int countColorsInput [ COLOR_COUNT ] ; int countColorsHidden [ COLOR_COUNT ] ; for ( int i = 0 ; i < COLOR_COUNT ; i ++ ) { countColorsInput [ i ] = 0 ; countColorsHidden [ i ] = 0 ; } for ( int i = 0 ; i < CODE_LENGTH ; i ++ ) { countColorsInput [ ( size_t ) input . colors [ i ] ] ++ ; countColorsHidden [ ( size_t ) hidden . colors [ i ] ] ++ ; } int matchingColors = 0 ; for ( int i = 0 ; i < COLOR_COUNT ; i ++ ) { int countInput = countColorsInput [ i ] ; int countHidden = countColorsHidden [ i ] ; matchingColors += ( countInput < countHidden ) ? countInput : countHidden ; } int fit = 0 ; for ( int i = 0 ; i < CODE_LENGTH ; i ++ ) { if ( input . colors [ i ] == hidden . colors [ i ] ) { fit ++ ; } } result . fit = fit ; result . misplaced = matchingColors - fit ; return result ; }
The heart of the game.
The heart of the game .
/*
     * This is actually rather complicated because of the
     * possibility of multiple occurences of the same color in the codes.
     *
     * This code uses the idea that the sum of 'fit' and 'misplaced' is equal
     * to the cardinality of the multiset-intersection of the codes viewed as multisets
     * by disregarding the order of the colors.
     *
     * Calculating 'fit' is easy. 'misplaced' is the difference of that
     * cardinality and 'fit'.
     */ //set to 0 //count colors //find how many matching colors are in input //maximum of matching colors is hidden's color count //find "fit" //misplaced is matchingColors which are not "fit"