Last active
April 18, 2019 01:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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