#!/usr/bin/ruby

# https://rosettacode.org/wiki/Best_shuffle#Sidef

func best_shuffle(original_word) {

    var s = original_word.chars;
    var t = s.shuffle;

    s.range.each { |i|
        s.range.each { |j|
            i == j || (
                  (t[i] == s[j]) ||
                  (t[j] == s[i])
            ) && next;
            t[i, j] = t[j, i];
            break;
        }
    }

    var word = t.join('');
    [word, original_word ^ word -> count("\0")];
}

<abracadabra seesaw elk grrrrrr up a>.each { |word|
    var (sword, score) = best_shuffle(word)...;
    "%s, %s: %d\n".printf(word, sword, score);
}
