Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 1.24 KB | None | 0 0
  1. sub strip-useless( $claim ) {
  2.     # takes an Elven Claim and reduces it to an array:
  3.     # [ x y x-offset y-offset ] where every element is Int
  4.  
  5.     my @stripped = $claim.split( " " );
  6.  
  7.     @stripped = @stripped[2..*];
  8.  
  9.     my @xy = @stripped[0].split( "," );
  10.  
  11.     my $y = @xy[1].comb.Array;
  12.  
  13.     $y.pop;
  14.  
  15.     @xy[1] = $y.join;
  16.  
  17.     my @offset = @stripped[1].split( "x" );
  18.  
  19.     @xy.append( @offset ).map: { .Int };
  20.  
  21. }
  22.  
  23. my $sampleid = "#183 @ 903,595: 21x25";
  24.    
  25. sub get-rect-coords( @rect ) {
  26.  
  27.     my ($x, $y, $x-offset, $y-offset) = @rect;
  28.  
  29.     $x-offset += $x;
  30.     $y-offset += $y;
  31.  
  32.     for $y..^$y-offset -> $cur-y {
  33.         for $x..^$x-offset -> $cur-x {
  34.  
  35.             take $cur-x ~ "x" ~ $cur-y;
  36.  
  37.         }
  38.     }
  39. }
  40.  
  41. sub toss-in-bag( %bag, @rect ) {
  42.     my @coords = gather get-rect-coords( @rect );
  43.     for @coords -> $c {
  44.         %bag{ $c }++;
  45.     }
  46.     say "Tossed @rect[0]x@rect[1] in the bag."
  47. }
  48.  
  49. sub process-claims( $filename ) {
  50.  
  51.     my @claims = $filename.IO.lines;
  52.     my %cloth;
  53.  
  54.     for @claims -> $claim {
  55.  
  56.         toss-in-bag( %cloth, strip-useless( $claim ) );
  57.  
  58.     }
  59.  
  60.     %cloth;
  61.  
  62. }
  63.  
  64. my %bag = process-claims( "claims.txt" );
  65.  
  66. my $duplicate-claims-list = %bag.values.grep: none 1;
  67.  
  68. my $num-dups = $duplicate-claims-list.elems;
  69.  
  70. say "Located $num-dups duplicate claims using a HASH!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement