#!/usr/bin/perl use warnings; use strict; my @drinks = qw(tea coffee milk water beer); my @pets = qw(dog bird cat horse fish); my @colors = qw(red blue yellow green white); my @countries = qw(British Swedish Danish German Norwegian); my @cigars = qw(PallMall BlueMaster Dunhill Blends Prince); my @homes = qw(1 2 3 4 5); my @pos = (); for my $drink (@drinks) { for my $pet (@pets) { for my $color (@colors) { for my $country (@countries) { for my $cigar (@cigars) { for my $home (@homes) { push @pos, " $home $drink $pet $color $country $cigar "; } } } } } } our $prop; my @ans = (); for(@pos) { $prop = $_; push @ans, $_ if 1 && f( qw(red British) ) != 1 && f( qw(dog Swedish) ) != 1 && f( qw(tea Danish) ) != 1 && f( qw(green coffee) ) != 1 && f( qw(PallMall bird) ) != 1 && f( qw(yellow Dunhill) ) != 1 && f( qw(Prince German) ) != 1 && f( qw(BlueMaster beer) ) != 1 && f( qw(milk 3) ) != 1 && f( qw(Norwegian 1) ) != 1 && f( qw(blue 2) ) != 1 && f( qw(Blends water) ) < 2 && f( qw(Norwegian blue) ) < 2 && f( qw(Blends cat) ) < 2 ; } my @house1 = grep {/ 1 /} @ans; my @house2 = grep {/ 2 /} @ans; my @house3 = grep {/ 3 /} @ans; my @house4 = grep {/ 4 /} @ans; my @house5 = grep {/ 5 /} @ans; for my $house1 (@house1) { next unless 6 == uniq($house1); for my $house2 (@house2) { next unless 12 == uniq($house1, $house2); for my $house3 (@house3) { next unless 18 == uniq($house1, $house2, $house3); for my $house4 (@house4) { next unless 24 == uniq($house1, $house2, $house3, $house4); for my $house5 (@house5) { next unless 30 == uniq($house1, $house2, $house3, $house4, $house5); my(@x, @y, $x, $y); @x = grep {/green/} ($house1,$house2,$house3,$house4,$house5); $x[0] =~ /^ (\d+)/; $x = $1; @y = grep {/white/} ($house1,$house2,$house3,$house4,$house5); $y[0] =~ /^ (\d+)/; $y = $1; next unless $x == $y - 1; @x = grep {/Blends/} ($house1,$house2,$house3,$house4,$house5); $x[0] =~ /^ (\d+)/; $x = $1; @y = grep {/cat/} ($house1,$house2,$house3,$house4,$house5); $y[0] =~ /^ (\d+)/; $y = $1; next unless abs($x - $y) == 1; @x = grep {/horse/} ($house1,$house2,$house3,$house4,$house5); $x[0] =~ /^ (\d+)/; $x = $1; @y = grep {/Dunhill/} ($house1,$house2,$house3,$house4,$house5); $y[0] =~ /^ (\d+)/; $y = $1; next unless abs($x - $y) == 1; my $matrix = "$house1\n$house2\n$house3\n$house4\n$house5\n"; print $matrix; }}}}} sub f { my $count = 0; for(@_) { ++$count if $prop =~ / $_ /; } for my $att (@_) { my $good = 0; for(@drinks, @pets, @colors, @countries, @cigars, @homes) { ++$good if $_ eq $att; } die "$att is bad" if $good != 1; } return $count; } sub uniq { local %_ = (); for(@_) { for(/\S+/g) { $_{$_} = 1; } } my @arr = keys %_; return scalar @arr; }