Skip to content

Instantly share code, notes, and snippets.

@ugexe
Last active April 18, 2019 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugexe/e20423ae8b7b00368f2f2f1b50730505 to your computer and use it in GitHub Desktop.
Save ugexe/e20423ae8b7b00368f2f2f1b50730505 to your computer and use it in GitHub Desktop.
Polyglot Perl5/6 print words that can be spelled from given input letters
# perl|perl6 words.pl word_list.txt f o o b a r b a z
my @ARGV = do { sub eval { &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) };
sub polyslurp ($_) { "0" and (return "{$_.IO.slurp}") or (return do { open(my $fh, $_[0]); join("", <$fh>); }) };
my $filename = shift(@ARGV);
my @words = split("\n", polyslurp($filename));
my $input_letters = {};
$input_letters{lc($_)} += 1 for @ARGV;
WORDS: for (@words) {
my @word_letters = grep &{ sub ($_) { $_ ne "" } }.(), split("", lc($_));
next unless +@word_letters;
my $letter_counter = {};
$letter_counter{$_} = 0 for @word_letters;
for (@word_letters) {
next WORDS unless ($input_letters{$_} // 0) > $letter_counter{$_}++;
}
$input_letters{$_}-- for @word_letters;
print "$_\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment