#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11116556 use warnings; use Path::Tiny; use 5.016; ### adding grown-up perl logging use Log::Log4perl; my $log_conf4 = "/home/hogan/Documents/hogan/logs/conf_files/4.conf"; # get rid of old one my $file = '/home/hogan/Documents/hogan/logs/4.log4perl.txt'; unlink $file or warn "Could not unlink $file: $!"; # init new one Log::Log4perl::init($log_conf4); #info my $logger = Log::Log4perl->get_logger(); $logger->info($0); my $dict_path = '/home/hogan/Documents/hogan/my_data/upwords.10'; my $dictionary = path($dict_path); my @tiles = map +( 'a' .. 'z' )[ rand 26 ], 1 .. 7; print "tiles: @tiles\n"; $logger->info("tiles: @tiles\n"); my $string = 'w r t'; $logger->info("============"); say "$string"; $logger->info($string); my $pat = join '', map "$_?", sort @tiles, $string =~ /\w/g; my $tilepat = join '', map "$_?", sort @tiles; my $letters = join '', @tiles, $string =~ /\w/g; print "$pat\n$tilepat\n"; $logger->info("$pat\ntilepat: $tilepat\nletters: $letters\n"); my @matches = grep { ( join '', sort split // ) =~ /^$pat$/ } $dictionary->slurp =~ /^[$letters]{2,}$/gm; my @places; ( $string =~ tr/ /./r ) =~ /(?info("matches1================\n @matches1"); my @matches2 = extension(2); print "@matches2\n\n"; $logger->info("matches2================\n @matches2"); sub extension { my $id = shift; my @found; for my $placepat ( $id == 1 ? @places : map expand($_), @places ) { for my $match (@matches) { $logger->info("id: $id match: $match"); $match =~ /^$placepat$/ or next; $logger->info("id: $id placepat: $placepat"); $logger->info("matched: $match"); my $newtiles = $match & ( $placepat =~ tr/.a-z/\xff\0/r ); my ($hex) = unpack( 'H*', $newtiles ); $logger->info("hex is: $hex"); ( join '', sort $newtiles =~ /\w/g ) =~ /^$tilepat$/ and push @found, $match; } } return @found; } sub expand { grep /\w/, glob join '', map { /\w/ ? "{$_,.}" : $_ } split //, shift; }