Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 1.92 KB | None | 0 0
  1. =begin plan
  2.  
  3. figure out how to parse the db
  4.  
  5. figure out how large the cloth is?
  6. or do it dynamically?
  7.  
  8. if i use a baghash i can use standardized coordinate names
  9. #179 @ 907,336: 26x10
  10.  
  11. basically here i'm starting with $x=907 and $y=336
  12. in the baghash, incr a key 907x336, through to 933x336,
  13. then 907x337 to 933x337
  14. ...
  15. 907x346 to 933x346
  16.  
  17. (should probably switch that so i'm mentally thinking about scanning horizontally)
  18.  
  19. anything that isn't claimed doesn't exist in the bag
  20. any coordinate that is claimed is in the bag with a value of at least 1
  21.  
  22. then i take the list of values, filter out the 1s, and give the length of the end list
  23.  
  24. =end plan
  25.  
  26. sub strip-useless( $claim ) {
  27.     # takes an Elven Claim and reduces it to an array:
  28.     # [ x y x-offset y-offset ] where every element is Int
  29.  
  30.     my @stripped = $claim.split( " " );
  31.  
  32.     @stripped = @stripped[2..*];
  33.  
  34.     my @xy = @stripped[0].split( "," );
  35.  
  36.     my $y = @xy[1].comb.Array;
  37.  
  38.     $y.pop;
  39.  
  40.     @xy[1] = $y.join;
  41.  
  42.     my @offset = @stripped[1].split( "x" );
  43.  
  44.     @xy.append( @offset ).map: { .Int };
  45.  
  46. }
  47.  
  48. my $sampleid = "#183 @ 903,595: 21x25";
  49.    
  50. sub get-rect-coords( @rect ) {
  51.  
  52.     my ($x, $y, $x-offset, $y-offset) = @rect;
  53.  
  54.     $x-offset += $x;
  55.     $y-offset += $y;
  56.  
  57.     for $y..^$y-offset -> $cur-y {
  58.         for $x..^$x-offset -> $cur-x {
  59.  
  60.             take $cur-x ~ "x" ~ $cur-y;
  61.  
  62.         }
  63.     }
  64. }
  65.  
  66. sub toss-in-bag( $bag is rw, @rect ) {
  67.     my @coords = gather get-rect-coords( @rect );
  68.     for @coords -> $c {
  69.         $bag{ $c }++;
  70.     }
  71.     say "Tossed @rect[0]x@rect[1] in the bag."
  72. }
  73.  
  74. sub process-claims( $filename ) {
  75.  
  76.     my @claims = $filename.IO.lines;
  77.     my BagHash $cloth .= new;
  78.  
  79.     for @claims -> $claim {
  80.  
  81.         toss-in-bag( $cloth, strip-useless( $claim ) );
  82.  
  83.     }
  84.  
  85.     $cloth;
  86.  
  87. }
  88.  
  89. my $bag = process-claims( "claims.txt" );
  90.  
  91. my $duplicate-claims-list = $bag.values.grep: none 1;
  92.  
  93. my $num-dups = $duplicate-claims-list.elems;
  94.  
  95. say "Located $num-dups duplicate claims using BAGHASH!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement