Created
May 24, 2019 12:30
-
-
Save kevincolyer/259b0cfb1230137a65f8465bcfb1ebc9 to your computer and use it in GitHub Desktop.
Perl Weekly challenge - week 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use v6; | |
say "Part 1 (first square number with 5 distinct digits)"; | |
for (100..10_000) -> $i { my $j=$i**2; "$i -> $j".say && last if $j.comb.unique.elems==5 }; | |
say "\nPart 2 (Rankings)"; | |
# Imagine a running race - times of the runners are: | |
my @times= 10, 5, 10, 15; | |
say "Standard: " ~ standard(@times); # 1,2,2,4 | |
say "Modified: " ~ modified(@times); # 1,3,3,4 | |
say "Dense: " ~ dense(@times); # 1,2,2,3 | |
sub standard(@t){ | |
my $rank=1; | |
my Bag $b=@t.Bag; # note @t.Bag.sort gives a sequence not a bag! | |
return gather for $b.keys.sort { | |
for ^$b{$_} { take $rank }; | |
$rank+=$b{$_}; | |
} | |
} | |
sub modified(@t){ | |
my $rank=0; # change start point | |
my $b=@t.Bag; | |
return gather for $b.keys.sort { | |
$rank+=$b{$_}; | |
for ^$b{$_} { take $rank }; | |
} | |
} | |
sub dense(@t){ | |
my $rank=1; | |
my $b=@t.Bag; | |
return gather for $b.keys.sort { | |
for ^$b{$_} { take $rank;}; | |
$rank++; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment