Skip to content

Instantly share code, notes, and snippets.

@adamcrussell

adamcrussell/ch-1.p

Last active Aug 30, 2020
Embed
What would you like to do?
Perl Weekly Challenge 075
member(X,[X|_]).
member(X,[_|T]) :- member(X,T).
coins([1,2,4]).
sum(Coins):-
sum([], Coins, 0).
sum(Coins, Coins, 6).
sum(Partial, Coins, Sum):-
Sum < 6,
coins(L),
member(X,L),
S is Sum + X,
sum([X | Partial], Coins, S).
main:-
findall(Coins,sum(Coins), C),
writeln(C),
halt.
use strict;
use warnings;
use AI::Prolog;
use Hash::MultiKey;
MAIN:{
my $S = $ARGV[0];
my $C = "[" . $ARGV[1] . "]";
my $prolog = do{
local $/;
<DATA>;
};
$prolog =~ s/_COINS_/$C/g;
$prolog =~ s/_SUM_/$S/g;
$prolog = new AI::Prolog($prolog);
$prolog->query("sum(Coins).");
my %h;
tie %h, "Hash::MultiKey";
while(my $result = $prolog->results){
my @s = sort @{$result->[1]};
$h{\@s} = undef;
}
for my $k (keys %h){
print "(" . join(",", @{$k}) . ")";
print "\n";
}
}
__DATA__
member(X,[X|_]).
member(X,[_|T]) :- member(X,T).
coins(_COINS_).
sum(Coins):-
sum([], Coins, 0).
sum(Coins, Coins, _SUM_).
sum(Partial, Coins, Sum):-
Sum < _SUM_,
coins(L),
member(X,L),
S is Sum + X,
sum([X | Partial], Coins, S).
use strict;
use warnings;
sub print_histogram{
my($values) = @_;
my @sorted_values = sort @{$values};
my $max = $sorted_values[-1];
my $x = $max;
while($x >= 1){
print "$x ";
for my $h (@{$values}){
print "# " if $h >= $x;
print " " if $h < $x;
}
print "\n";
$x--;
}
print "- " x (@{$values} + 1);
print "\n " . join(" ", @{$values}) ."\n";
}
MAIN:{
#my @A = (2, 1, 4, 5, 3, 7);
my @A = (3, 2, 3, 5, 7, 5);
print_histogram(\@A);
my $maximum_rectangle = -1;
for my $i (@A){
my $rectangle;
for my $j (@A){
if($i > $j){
$rectangle = 0;
}
if($i <= $j){
$rectangle += $i;
}
}
$maximum_rectangle = $rectangle if $rectangle > $maximum_rectangle;
}
print "Largest Rectangle: $maximum_rectangle\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.