use strict; use warnings; if (!@ARGV) { print < q" I will deduce the correct conclusion to be made using basic logic and the chain rule. - may be used for negation q's can not be duplicated HELP exit; } LHCC(parse(@ARGV)); sub LHCC { my (%all) = @_; my @Conclusion; my @alternate; foreach my $testKey (keys %all) { my @candidate = ($testKey); my $nextKey = $testKey; while ($nextKey) { $nextKey = $all{$nextKey}; push @candidate, $nextKey; last if ! exists $all{$nextKey}; } if (scalar @candidate > scalar @Conclusion) { @Conclusion = @candidate; } elsif (scalar @candidate == scalar @Conclusion) { @alternate = @candidate; } } print join ' => ', @Conclusion; print "\nor\n"; print join ' => ', @alternate; print < $Conclusion[-1] or: $alternate[0] => $alternate[-1] RESULT } sub parse { my %all; for my $statement (@_) { my ($p, $q) = $statement =~ /(\S+)\s*->\s*(\S+)/; $all{$p} = $q; $_ = /^-(.*)/ ? $1 : "-$_" for $p, $q; $all{$q} = $p; } return %all; } #### p => f => c => -b => -g or g => b => -c => -f => -p In other words: p => -g or: g => -p