in reply to Re^5: My first cpan module - App::ForKids::LogicalPuzzleGenerator
in thread My first cpan module - App::ForKids::LogicalPuzzleGenerator
Here's the simpler problem solved by regex.
#!/usr/bin/perl use strict; use warnings; # Three friends live here. # Each likes a different fruit and has a different profession. # John doesn't like pears. # The programmer likes cherries. # Patrick is a blacksmith. # Edward isn't a fisherman. # Who likes apples? $_ = <<END; John apples,cherries,pears blacksmith,fisherman,programmer Patrick apples,cherries,pears blacksmith,fisherman,programmer Edward apples,cherries,pears blacksmith,fisherman,programmer END my @answer = /^ (\w+)\ \S*\b(\w+)\b\S*\ \S*\b(\w+)\b\S*\n # first line (\w+)\ \S*\b(\w+)\b(??{$2 eq $5})\S*\ # second line \S*\b(\w+)\b(??{$3 eq $6})\S*\n (\w+)\ # third line \S*\b(\w+)\b(??{$5 eq $8 || $2 eq $8})\S*\ \S*\b(\w+)\b(??{$6 eq $9 || $3 eq $9})\S*\n (??{ $2 eq 'pears' }) # John doesn't like pears (??{ $6 ne 'blacksmith' }) # Patrick is a blacksmith (??{ !grep $_ eq 'cherriesprogrammer', # The programmer likes cherr +ies $2.$3, $4.$5, $8.$9 }) (??{ $9 eq 'fisherman' }) # Edward isn't a fisherman /x or die "failed"; print "$1 $2 $3\n$4 $5 $6\n$7 $8 $9\n\n"; print "@answer[ map $_ - 1, grep $answer[$_] eq 'apples', 0 .. $#answer] likes apples.\n";
outputs:
John apples fisherman Patrick pears blacksmith Edward cherries programmer John likes apples.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: My first cpan module - App::ForKids::LogicalPuzzleGenerator
by LanX (Saint) on Feb 27, 2018 at 09:33 UTC | |
by tybalt89 (Monsignor) on Feb 27, 2018 at 10:24 UTC |