Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Last active August 30, 2020 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrussell/0c0dfc84c4a18dd8261728d062f3c2f5 to your computer and use it in GitHub Desktop.
Save adamcrussell/0c0dfc84c4a18dd8261728d062f3c2f5 to your computer and use it in GitHub Desktop.
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