my @words = '/usr/share/dict/words'.IO.words.grep(*.chars == 5)».lc.grep({ $_.comb.all eq any('a'..'z') });
loop {
my @guess;
repeat {
my $guess = prompt 'your guess: ';
exit if $guess eq 'q';
@guess = $guess.comb;
} until is-valid-guess(@guess);
my @feedback;
repeat {
my $feedback = prompt 'feedback: ';
exit if $feedback eq @guess.join;
@feedback = $feedback.comb;
} until is-valid-feedback(@feedback);
for ^5 -> \n {
given @feedback[n] {
when 'a'..'z' { @words .= grep({ .substr(n,1) eq @feedback[n] }) }
when '+' { @words .= grep({ .contains(@guess[n]) and .substr(n,1) ne @guess[n] }) }
when '-' { @words .= grep({ ! .contains(@guess[n]) }) }
}
}
put @words.join(' ');
put @words.elems, ' candidates';
}
sub is-valid-guess(@guess --> Bool) {
@guess.elems eq 5 and so @guess.all eq ('a'..'z').any
}
sub is-valid-feedback(@fb --> Bool) {
@fb.elems eq 5 and so @fb.all eq (|('a'..'z'), '+', '-').any
}