aboutsummaryrefslogtreecommitdiff
path: root/challenge-057/javier-luque/raku/ch-2.p6
blob: ba7b3f299d6f9ca51441bde2f67a94320b926a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Test: perl6 ch-2.p6 4
use Concurrent::Trie;

sub MAIN() {
    my $trie = Concurrent::Trie.new;
    my @words = qw[
    	alphabet  book    carpet
    	cadmium   cadeau  alpine
    ];

    # Insert each word
    for (@words) -> $word {
    	$trie.insert($word)
    }

    # Find each answer
    my @answers;
    for (@words) -> $word {
    	my $search_word = '';

    	for ($word.comb) -> $letter {
    		$search_word ~= $letter;
    		my @counts = $trie.entries($search_word);

    		# If there is only one word left,
    		# we are unique
    		if (@counts.elems == 1) {
    			@answers.push($search_word);
    			last;
    		}
    	}
    }

    say @answers.perl;
}