saltycracker

EdgeBuilder.pm

Nov 1st, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.38 KB | None | 0 0
  1. package SaltyCracker::EdgeBuilder;
  2.  
  3. use 5.006;
  4. use strict;
  5. use warnings;
  6. use constant BOX_SIZE => 9;
  7. use List::Util qw<any uniq>;
  8.  
  9. use parent qw<Exporter>;
  10.  
  11. our @EXPORT = qw<>;
  12. our @EXPORT_OK = qw<getEdges>;
  13. our %EXPORT_TAGS = (ALL => [@EXPORT, @EXPORT_OK]);
  14.  
  15. =head1 NAME
  16.  
  17. SaltyCracker::EdgeBuilder - The great new SaltyCracker::EdgeBuilder!
  18.  
  19. =head1 VERSION
  20.  
  21. Version 0.01
  22.  
  23. =cut
  24.  
  25. our $VERSION = '0.01';
  26.  
  27. my $edges;
  28.  
  29. =head1 SYNOPSIS
  30.  
  31. Quick summary of what the module does.
  32.  
  33. Perhaps a little code snippet.
  34.  
  35.     use SaltyCracker::EdgeBuilder;
  36.  
  37.     my $foo = SaltyCracker::EdgeBuilder->new();
  38.     ...
  39.  
  40. =head1 EXPORT
  41.  
  42. A list of functions that can be exported.  You can delete this section
  43. if you don't export anything, such as for a purely object-oriented module.
  44.  
  45. =head1 SUBROUTINES/METHODS
  46.  
  47. =head2 getEdges
  48.  
  49. =cut
  50.  
  51. sub getEdges {
  52.   buildEdges();
  53.   $edges;
  54. }
  55.  
  56.  
  57. =head2 buildEdges
  58.  
  59. =cut
  60.  
  61. sub buildEdges {
  62.   for (my $i = 1; $i <= BOX_SIZE; ++$i) {
  63.     for (my $j = 1; $j <= BOX_SIZE; ++$j) {
  64.       my @n;
  65.       for (my $k = 1; $k <= BOX_SIZE; ++$k) {
  66.         push(@n, int($i.$k)) unless $i.$j == $i.$k;
  67.       }
  68.       push(@$edges, [int($i.$j),\@n]);
  69.     }
  70.   }
  71.   for my $elem (@$edges) {
  72.     my $a = $elem->[1];
  73.     my @s = split//,$elem->[0];
  74.     for (my $i = 1; $i <= BOX_SIZE; ++$i) {
  75.       my $n = int($i.$s[1]);
  76.       push(@$a, $n) unless $n == $elem->[0];
  77.     }
  78.   }
  79.   my $sr = sqrt(BOX_SIZE);
  80.   my $boxArr;
  81.   my $buildBoxArr;
  82.   for (my $i = 1; $i <= BOX_SIZE; ++$i) {
  83.     for (my $j = 1; $j <= BOX_SIZE; ++$j) {
  84.       push(@$buildBoxArr, $i.$j);
  85.       if ($j % $sr == 0) {
  86.         push(@$boxArr, $buildBoxArr);
  87.         $buildBoxArr = undef;
  88.       }
  89.     }
  90.   }
  91.   my @sortedBoxArr = sort {($a->[0] % 10) <=> ($b->[0] % 10)} @$boxArr;
  92.   my $counter = 0;  
  93.   $boxArr = undef;
  94.   $buildBoxArr = undef;
  95.   for my $elem (@sortedBoxArr) {
  96.     if ($counter >= $sr) {
  97.       push(@$boxArr, $buildBoxArr);
  98.       $counter = 0;
  99.       $buildBoxArr = undef;
  100.     }
  101.     push(@$buildBoxArr, @$elem);
  102.     ++$counter;
  103.   }
  104.   push(@$boxArr, $buildBoxArr);
  105.   for my $e (@$edges) {
  106.     my $pos = $e->[0];
  107.     my $e_arr = $e->[1];
  108.     for my $b (@$boxArr) {
  109.       if (any {$pos == $_} @$b) {
  110.           for my $ait (@$b) {
  111.             push(@$e_arr, $ait) unless $pos == $ait;
  112.           }
  113.           @$e_arr = uniq(@$e_arr);
  114.         last;
  115.       }
  116.     }
  117.   }
  118. }
  119.  
  120. =head1 AUTHOR
  121.  
  122. SaltyCracker, C<< <SaltyCracker> >>
  123.  
  124. =head1 BUGS
  125.  
  126. Please report any bugs or feature requests to C<bug-saltycracker-sudoku at rt.cpan.org>, or through
  127. the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SaltyCracker-Sudoku>.  I will be notified, and then you'll
  128. automatically be notified of progress on your bug as I make changes.
  129.  
  130.  
  131.  
  132.  
  133. =head1 SUPPORT
  134.  
  135. You can find documentation for this module with the perldoc command.
  136.  
  137.     perldoc SaltyCracker::EdgeBuilder
  138.  
  139.  
  140. You can also look for information at:
  141.  
  142. =over 4
  143.  
  144. =item * RT: CPAN's request tracker (report bugs here)
  145.  
  146. L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SaltyCracker-Sudoku>
  147.  
  148. =item * AnnoCPAN: Annotated CPAN documentation
  149.  
  150. L<http://annocpan.org/dist/SaltyCracker-Sudoku>
  151.  
  152. =item * CPAN Ratings
  153.  
  154. L<http://cpanratings.perl.org/d/SaltyCracker-Sudoku>
  155.  
  156. =item * Search CPAN
  157.  
  158. L<http://search.cpan.org/dist/SaltyCracker-Sudoku/>
  159.  
  160. =back
  161.  
  162.  
  163. =head1 ACKNOWLEDGEMENTS
  164.  
  165.  
  166. =head1 LICENSE AND COPYRIGHT
  167.  
  168. Copyright 2020 SaltyCracker.
  169.  
  170. This program is free software; you can redistribute it and/or modify it
  171. under the terms of the the Artistic License (2.0). You may obtain a
  172. copy of the full license at:
  173.  
  174. L<http://www.perlfoundation.org/artistic_license_2_0>
  175.  
  176. Any use, modification, and distribution of the Standard or Modified
  177. Versions is governed by this Artistic License. By using, modifying or
  178. distributing the Package, you accept this license. Do not use, modify,
  179. or distribute the Package, if you do not accept this license.
  180.  
  181. If your Modified Version has been derived from a Modified Version made
  182. by someone other than you, you are nevertheless required to ensure that
  183. your Modified Version complies with the requirements of this license.
  184.  
  185. This license does not grant you the right to use any trademark, service
  186. mark, tradename, or logo of the Copyright Holder.
  187.  
  188. This license includes the non-exclusive, worldwide, free-of-charge
  189. patent license to make, have made, use, offer to sell, sell, import and
  190. otherwise transfer the Package with respect to any patent claims
  191. licensable by the Copyright Holder that are necessarily infringed by the
  192. Package. If you institute patent litigation (including a cross-claim or
  193. counterclaim) against any party alleging that the Package constitutes
  194. direct or contributory patent infringement, then this Artistic License
  195. to you shall terminate on the date that such litigation is filed.
  196.  
  197. Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
  198. AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  199. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  200. PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
  201. YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
  202. CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
  203. CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
  204. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  205.  
  206.  
  207. =cut
  208.  
  209. 1; # End of SaltyCracker::EdgeBuilder
Add Comment
Please, Sign In to add comment