Online File

How to use this page

Rick Aster: Professional SAS Programming Shortcuts: Contents

Chapter 88
Sudoku validator

data _null_;
   * Read proposed solution from text file. *;
   array sol{81} $ 1 sol1-sol81;
   infile solution missover;
   input (sol{*}) (9*$char1. /);
   putlog 'Testing Sudoku solution:' // (sol{*}) (9*$char1. /) /;
   * Cells in the 27 domains to test. *;
   array domaincell{3, 9, 9} $ 1 sol1-sol81
       sol1 sol10 sol19 sol28 sol37 sol46 sol55 sol64 sol73
       sol2 sol11 sol20 sol29 sol38 sol47 sol56 sol65 sol74
       sol3 sol12 sol21 sol30 sol39 sol48 sol57 sol66 sol75
       sol4 sol13 sol22 sol31 sol40 sol49 sol58 sol67 sol76
       sol5 sol14 sol23 sol32 sol41 sol50 sol59 sol68 sol77
       sol6 sol15 sol24 sol33 sol42 sol51 sol60 sol69 sol78
       sol7 sol16 sol25 sol34 sol43 sol52 sol61 sol70 sol79
       sol8 sol17 sol26 sol35 sol44 sol53 sol62 sol71 sol80
       sol9 sol18 sol27 sol36 sol45 sol54 sol63 sol72 sol81
       sol1 sol2 sol3 sol10 sol11 sol12 sol19 sol20 sol21
       sol4 sol5 sol6 sol13 sol14 sol15 sol22 sol23 sol24
       sol7 sol8 sol9 sol16 sol17 sol18 sol25 sol26 sol27
       sol28 sol29 sol30 sol37 sol38 sol39 sol46 sol47 sol48
       sol31 sol32 sol33 sol40 sol41 sol42 sol49 sol50 sol51
       sol34 sol35 sol36 sol43 sol44 sol45 sol52 sol53 sol54
       sol55 sol56 sol57 sol64 sol65 sol66 sol73 sol74 sol75
       sol58 sol59 sol60 sol67 sol68 sol69 sol76 sol77 sol78
       sol61 sol62 sol63 sol70 sol71 sol72 sol79 sol80 sol81;
   array domaintypetext{3} $ 11 _temporary_
       ('row', ' column', '3x3 subgrid');
   * Counts of each number in each domain. *;
   array numbercount{9};
   * List of characters to count. *;
   retain numberlist '123456789';
   * Test each domain. *;
   do domaintype = 1 to 3; 
      do domain = 1 to 9;
         * Reset counts. *;
         do n = 1 to 9;
            numbercount{n} = 0;
         * Count the number found in each cell *;
         do cell = 1 to 9;
            cellchar = domaincell{domaintype, domain, cell};
            do n = 1 to 9;
               if cellchar = char(numberlist, n) then numbercount{n} + 1;
         * Check counts. Each number should occur 1 time. *;
         do n = 1 to 9;
            if numbercount{n} = 0 then do;
               * Stop with failure message. *;
               putlog 'Solution is incorrect.'
                   / n 'was not found in '
                   domaintypetext{domaintype} domain +(-1) '.';
      end; * End of loop. All domains checked out OK. *;
   putlog 'Puzzle is solved.';

 O /\





Tech | Dictionary

Download | Rastinate

Rick Aster

Professional SAS Programming Shortcuts

Contents/Online Files


Catalog Page