Task: Wordle comparison

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

void wordle(const char *answer, const char *guess, int *result) {
    int i, ix, n = strlen(guess);
    char *ptr;
    if (n != strlen(answer)) {
        printf("The words must be of the same length.\n");
        exit(1);
    }
    char answer2[n+1];
    strcpy(answer2, answer);
    for (i = 0; i < n; ++i) {
        if (guess[i] == answer2[i]) {
            answer2[i] = '\v';
            result[i] = 2;
        }
    }
    for (i = 0; i < n; ++i) {
        if ((ptr = strchr(answer2, guess[i])) != NULL) {
            ix = ptr - answer2;
            answer2[ix] = '\v';
            result[i] = 1;
        }
    }
}

int main() {
    int i, j;
    const char *answer, *guess;
    int res[5];
    const char *res2[5];
    const char *colors[3] = {"grey", "yellow", "green"};
    const char *pairs[5][2] = {
        {"ALLOW", "LOLLY"},
        {"BULLY", "LOLLY"},
        {"ROBIN", "ALERT"},
        {"ROBIN", "SONIC"},
        {"ROBIN", "ROBIN"}
    };
    for (i = 0; i < 5; ++i) {
        answer = pairs[i][0];
        guess  = pairs[i][1];
        for (j = 0; j < 5; ++j) res[j] = 0;
        wordle(answer, guess, res);
        for (j = 0; j < 5; ++j) res2[j] = colors[res[j]];
        printf("%s v %s => { ", answer, guess);
        for (j = 0; j < 5; ++j) printf("%d ", res[j]);
        printf("} => { ");
        for (j = 0; j < 5; ++j) printf("%s ", res2[j]);
        printf("}\n");
    }
    return 0;
}







Python: 

from functools import reduce
from operator import add



def wordleScore(target, guess):
    
    return mapAccumL(amber)(
        *first(charCounts)(
            mapAccumL(green)(
                [], zip(target, guess)
            )
        )
    )[1]



def green(residue, tg):
    
    t, g = tg
    return (residue, (g, 2)) if t == g else (
        [t] + residue, (g, 0)
    )



def amber(tally, cn):
    
    c, n = cn
    return (tally, 2) if 2 == n else (
        adjust(
            lambda x: x - 1,
            c, tally
        ),
        1
    ) if 0 < tally.get(c, 0) else (tally, 0)




def main():
    
    print(' -> '.join(['Target', 'Guess', 'Scores']))
    print()
    print(
        '\n'.join([
            wordleReport(*tg) for tg in [
                ("ALLOW", "LOLLY"),
                ("CHANT", "LATTE"),
                ("ROBIN", "ALERT"),
                ("ROBIN", "SONIC"),
                ("ROBIN", "ROBIN"),
                ("BULLY", "LOLLY"),
                ("ADAPT", "SÅLÅD"),
                ("Ukraine", "Ukraíne"),
                ("BBAAB", "BBBBBAA"),
                ("BBAABBB", "AABBBAA")
            ]
        ])
    )



def wordleReport(target, guess):
    
    scoreName = {2: 'green', 1: 'amber', 0: 'gray'}

    if 5 != len(target):
        return f'{target}: Expected 5 character target.'
    elif 5 != len(guess):
        return f'{guess}: Expected 5 character guess.'
    else:
        scores = wordleScore(target, guess)
        return ' -> '.join([
            target, guess, repr(scores),
            ' '.join([
                scoreName[n] for n in scores
            ])
        ])





def adjust(f, k, dct):
    
    return dict(
        dct,
        **{k: f(dct[k]) if k in dct else None}
    )



def charCounts(s):
    
    return reduce(
        lambda a, c: insertWith(add)(c)(1)(a),
        list(s),
        {}
    )



def first(f):
    
    return lambda xy: (f(xy[0]), xy[1])




def insertWith(f):
    
    return lambda k: lambda x: lambda dct: dict(
        dct,
        **{k: f(dct[k], x) if k in dct else x}
    )




def mapAccumL(f):
    
    def nxt(a, x):
        return second(lambda v: a[1] + [v])(
            f(a[0], x)
        )
    return lambda acc, xs: reduce(
        nxt, xs, (acc, [])
    )



def second(f):
    
    return lambda xy: (xy[0], f(xy[1]))



if __name__ == '__main__':
    main()