saltycracker

Sudoku9x9.pl

Nov 1st, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.44 KB | None | 0 0
  1. package SaltyCracker::Sudoku;
  2.  
  3. use 5.006;
  4. use strict;
  5. use warnings;
  6. use autodie;
  7. use feature qw<say>;
  8. use boolean;
  9. use List::Util qw<any>;
  10. use Data::Dumper;
  11. use constant SUDOKU_LEN => 81;
  12. use constant SUDOKU_VALID_CHARACTERS => [0, 1..9];
  13. use constant SUDOKU_NUMBERS => [1..9];
  14.  
  15. use SaltyCracker::EdgeBuilder qw<:ALL>;
  16.  
  17. use parent qw<Exporter>;
  18.  
  19. our @EXPORT = qw<>;
  20. our @EXPORT_OK = qw<getSudokuData>;
  21. our %EXPORT_TAGS = (ALL => [@EXPORT, @EXPORT_OK]);
  22.  
  23. =head1 NAME
  24.  
  25. SaltyCracker::Sudoku - The great new SaltyCracker::Sudoku!
  26.  
  27. =head1 VERSION
  28.  
  29. Version 0.01
  30.  
  31. =cut
  32.  
  33. our $VERSION = '0.01';
  34.  
  35. my $sudokuString;
  36. my @sudokuData;
  37. my $edges_ref;
  38.  
  39. my %p_to_elem = (
  40.   0 => 11, 1 => 12, 2 => 13,
  41.   3 => 14, 4 => 15, 5 => 16,
  42.   6 => 17, 7 => 18, 8 => 19,
  43.  
  44.   9 => 21, 10 => 22, 11 => 23,
  45.   12 => 24, 13 => 25, 14 => 26,
  46.   15 => 27, 16 => 28, 17 => 29,
  47.  
  48.   18 => 31, 19 => 32, 20 => 33,
  49.   21 => 34, 22 => 35, 23 => 36,
  50.   24 => 37, 25 => 38, 26 => 39,
  51.  
  52.   27 => 41, 28 => 42, 29 => 43,
  53.   30 => 44, 31 => 45, 32 => 46,
  54.   33 => 47, 34 => 48, 35 => 49,
  55.  
  56.   36 => 51, 37 => 52, 38 => 53,
  57.   39 => 54, 40 => 55, 41 => 56,
  58.   42 => 57, 43 => 58, 44 => 59,
  59.  
  60.   45 => 61, 46 => 62, 47 => 63,
  61.   48 => 64, 49 => 65, 50 => 66,
  62.   51 => 67, 52 => 68, 53 => 69,
  63.  
  64.   54 => 71, 55 => 72, 56 => 73,
  65.   57 => 74, 58 => 75, 59 => 76,
  66.   60 => 77, 61 => 78, 62 => 79,
  67.  
  68.   63 => 81, 64 => 82, 65 => 83,
  69.   66 => 84, 67 => 85, 68 => 86,
  70.   69 => 87, 70 => 88, 71 => 89,
  71.  
  72.   72 => 91, 73 => 92, 74 => 93,
  73.   75 => 94, 76 => 95, 77 => 96,
  74.   78 => 97, 79 => 98, 80 => 99,
  75. );
  76.  
  77. my %elem_to_p = reverse %p_to_elem;
  78.  
  79. =head1 SYNOPSIS
  80.  
  81. Quick summary of what the module does.
  82.  
  83. Perhaps a little code snippet.
  84.  
  85.     use SaltyCracker::Sudoku;
  86.  
  87.     my $foo = SaltyCracker::Sudoku->new();
  88.     ...
  89.  
  90. =head1 EXPORT
  91.  
  92. A list of functions that can be exported.  You can delete this section
  93. if you don't export anything, such as for a purely object-oriented module.
  94.  
  95. =head1 SUBROUTINES/METHODS
  96.  
  97. =head2 checkSudokuDataLength
  98.  
  99. =cut
  100.  
  101. sub checkSudokuDataLength {
  102.   my $len = @sudokuData;
  103.   say "Validating sudoku length...";
  104.   if ($len != SUDOKU_LEN) {
  105.     die "[Failed] - Validating sudoku length failed...\n";
  106.   }
  107.   say "[Passed] - Validating sudoku length";
  108. }
  109. #;
  110. =head2 checkSudokuData
  111.  
  112. =cut
  113.  
  114. sub checkSudokuData {
  115.   my $failed = false;
  116.   my $valid_characters = SUDOKU_VALID_CHARACTERS;
  117.   say "Validating sudoku characters...";
  118.   while (my ($index, $elem) = each @sudokuData) {
  119.     sub
  120.     {
  121.       say "$elem at position $index is invalid\n";
  122.       $failed = true;
  123.       } ->() unless any {$_ == $elem} @$valid_characters;
  124.   }
  125.   die "Found invalid characters... Exiting\n" if $failed;
  126.   say "[Passed] - Validating sudoku characters";
  127. }
  128.  
  129. =head2 getStartNumbers
  130.  
  131. =cut
  132.  
  133. sub getStartNumbers {
  134.   my ($n) = @_;
  135.   my @numbers;
  136.   my $valid_numbers = SUDOKU_NUMBERS;
  137.   my @numbers_final;
  138.   for my $elem (@$n) {
  139.     if ($$elem) {
  140.       push(@numbers, $elem);
  141.     }
  142.   }
  143.   for my $elem (@$valid_numbers) {
  144.       push(@numbers_final, $elem) unless any {$elem == $$_} @numbers;
  145.   }
  146.   \@numbers_final;
  147. }
  148.  
  149. =head2 buildEdgesRef
  150.  
  151. =cut
  152.  
  153. sub buildEdgesRef {
  154.   my $edges = getEdges();
  155.   while (my ($index, $key) = each @sudokuData) {
  156.     my @arr;
  157.     for my $e (@{$edges->[$index]->[1]}) {
  158.       push(@arr, \$sudokuData[$elem_to_p{$e}]);
  159.     }
  160.     $edges_ref->{$p_to_elem{$index}} = {
  161.     references => \@arr,
  162.     value => $key,
  163.     fixed => $key == 0 ? false : true,
  164.     startNums => getStartNumbers(\@arr)
  165.     };
  166.   }
  167. }
  168.  
  169. =head2 solveSudokuPuzzle
  170.  
  171. =cut
  172.  
  173. sub solveSudokuPuzzle {
  174.   my @keys = sort {$a <=> $b} (keys %$edges_ref);
  175.   my $pos = 0;
  176.  
  177.   if ($edges_ref->{$p_to_elem{$pos}}->{fixed}) {
  178.     solveSudokuPuzzleAux($pos + 1);
  179.   }else {
  180.     my $a_refer = $edges_ref->{$p_to_elem{$pos}}->{references};
  181.     my $start_nums = $edges_ref->{$p_to_elem{$pos}}->{startNums};
  182.     for my $e (@$start_nums) {
  183.       my $old_data = $sudokuData[$pos];
  184.       $sudokuData[$pos] = $e;
  185.       solveSudokuPuzzleAux($pos + 1);
  186.       $sudokuData[$pos] = $old_data;
  187.     }
  188.   }
  189. }
  190.  
  191. =head2 solveSudokuPuzzleAux
  192.  
  193. =cut
  194.  
  195. sub solveSudokuPuzzleAux {
  196.   my ($pos) = @_;
  197.   if ($pos < SUDOKU_LEN) {
  198.     if ($edges_ref->{$p_to_elem{$pos}}->{fixed}) {
  199.       solveSudokuPuzzleAux($pos + 1);
  200.     } else {
  201.       my $a_refer = $edges_ref->{$p_to_elem{$pos}}->{references};
  202.       my $start_nums = $edges_ref->{$p_to_elem{$pos}}->{startNums};
  203.       for my $e (@$start_nums) {
  204.         next if any {$e == $$_} (@$a_refer);
  205.         my $old_data = $sudokuData[$pos];
  206.         $sudokuData[$pos] = $e;
  207.         solveSudokuPuzzleAux($pos + 1);
  208.         $sudokuData[$pos] = $old_data;
  209.       }
  210.     }
  211.   }else {
  212.     say "--------------->Found a solution<----------------------";
  213.     for (my $i = 0; $i < SUDOKU_LEN; ++$i) {
  214.       if ($i % 27 == 0) {
  215.         print "\n\n\n";
  216.       }elsif ($i % 9 == 0) {
  217.         print "\n";
  218.       }elsif ($i % 3 == 0) {
  219.         print "   ";
  220.       }
  221.       print $sudokuData[$i], " ";
  222.     }
  223.     print "\n\n\n\n";
  224.   }
  225. }
  226.  
  227. =head2 getSudokuData
  228.  
  229. =cut
  230.  
  231. sub getSudokuData {
  232.   my ($filename) = @_;
  233.   open(my $iFile, '<:encoding(utf-8)', $filename);
  234.   my $sudokuStr;
  235.   {
  236.     local $/ = undef;
  237.     $sudokuStr = <$iFile>;
  238.   }
  239.   chomp($sudokuStr);
  240.   @sudokuData = split//,$sudokuStr;
  241.   checkSudokuDataLength();
  242.   checkSudokuData();
  243.   $sudokuString = $sudokuStr;
  244.   buildEdgesRef();
  245.   solveSudokuPuzzle();
  246. }
  247.  
  248. =head2 function2
  249.  
  250. =cut
  251.  
  252. sub function2 {
  253. }
  254.  
  255. =head1 AUTHOR
  256.  
  257. SaltyCracker, C<< <SaltyCracker> >>
  258.  
  259. =head1 BUGS
  260.  
  261. Please report any bugs or feature requests to C<bug-saltycracker-sudoku at rt.cpan.org>, or through
  262. the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SaltyCracker-Sudoku>.  I will be notified, and then you'll
  263. automatically be notified of progress on your bug as I make changes.
  264.  
  265.  
  266.  
  267.  
  268. =head1 SUPPORT
  269.  
  270. You can find documentation for this module with the perldoc command.
  271.  
  272.     perldoc SaltyCracker::Sudoku
  273.  
  274.  
  275. You can also look for information at:
  276.  
  277. =over 4
  278.  
  279. =item * RT: CPAN's request tracker (report bugs here)
  280.  
  281. L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SaltyCracker-Sudoku>
  282.  
  283. =item * AnnoCPAN: Annotated CPAN documentation
  284.  
  285. L<http://annocpan.org/dist/SaltyCracker-Sudoku>
  286.  
  287. =item * CPAN Ratings
  288.  
  289. L<http://cpanratings.perl.org/d/SaltyCracker-Sudoku>
  290.  
  291. =item * Search CPAN
  292.  
  293. L<http://search.cpan.org/dist/SaltyCracker-Sudoku/>
  294.  
  295. =back
  296.  
  297.  
  298. =head1 ACKNOWLEDGEMENTS
  299.  
  300.  
  301. =head1 LICENSE AND COPYRIGHT
  302.  
  303. Copyright 2020 SaltyCracker.
  304.  
  305. This program is free software; you can redistribute it and/or modify it
  306. under the terms of the the Artistic License (2.0). You may obtain a
  307. copy of the full license at:
  308.  
  309. L<http://www.perlfoundation.org/artistic_license_2_0>
  310.  
  311. Any use, modification, and distribution of the Standard or Modified
  312. Versions is governed by this Artistic License. By using, modifying or
  313. distributing the Package, you accept this license. Do not use, modify,
  314. or distribute the Package, if you do not accept this license.
  315.  
  316. If your Modified Version has been derived from a Modified Version made
  317. by someone other than you, you are nevertheless required to ensure that
  318. your Modified Version complies with the requirements of this license.
  319.  
  320. This license does not grant you the right to use any trademark, service
  321. mark, tradename, or logo of the Copyright Holder.
  322.  
  323. This license includes the non-exclusive, worldwide, free-of-charge
  324. patent license to make, have made, use, offer to sell, sell, import and
  325. otherwise transfer the Package with respect to any patent claims
  326. licensable by the Copyright Holder that are necessarily infringed by the
  327. Package. If you institute patent litigation (including a cross-claim or
  328. counterclaim) against any party alleging that the Package constitutes
  329. direct or contributory patent infringement, then this Artistic License
  330. to you shall terminate on the date that such litigation is filed.
  331.  
  332. Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
  333. AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  334. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  335. PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
  336. YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
  337. CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
  338. CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
  339. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  340.  
  341.  
  342. =cut
  343.  
  344. 1; # End of SaltyCracker::Sudoku
Add Comment
Please, Sign In to add comment