%%
%% This is file `createsudoku.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% sudokubundle.dtx  (with options: `create')
%% 
%% -----------------------------------------------------------------
%%   Author: Peter Wilson (herries dot press at earthlink dot net)
%%           Herries Press
%%   Copyright 2006 Peter R. Wilson
%% 
%%   This work may be distributed and/or modified under the
%%   conditions of the LaTeX Project Public License, either
%%   version 1.3 of this license or (at your option) any
%%   later version.
%%   The latest version of the license is in
%%      http://www.latex-project.org/lppl.txt
%%   and version 1.3 or later is part of all distributions of
%%   LaTeX version 2003/06/01 or later.
%% 
%%   This work has the LPPL maintenance status "author-maintained".
%% 
%%   This work consists of the files listed in the README file.
%% -----------------------------------------------------------------
%% 
%%%% solvethem.tex   Solve Sudoku files
%%%%                 Peter Wilson
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{createsudoku}[2006/02/16 v1.0 create sudoku puzzles]
\input{random}
\RequirePackage{solvesudoku}

\let\oldcommentary\commentary
\let\commentary\@gobble
\newcommand*{\gencommentary}[1]{\typeout{#1}}

\newcommand*{\genfile}{gensud.sud}
\newcommand*{\prevfile}{genprev.sdx}
\newcommand*{\currfile}{gencurr.sdx}

\randomi\m@ne
\newcommand*{\setsudrandom}[1]{%
  \randomi=#1\relax
  \ifnum\randomi<\@ne\else
    \nextrandom \nextrandom \nextrandom
    \typeout{random initialised to #1}%
  \fi}

\newcommand*{\generategrid}[1][\@empty]{%
  \ifx\@empty #1
    \writestartgrid
    \getproblem{\prevfile}
  \else
    \getproblem{#1}
  \fi
  \swaps
  \elimclues
  \printsudresults}
\newcommand*{\printsudresults}{%
  \sudokusolve{\genfile}%
  }

%% the starting data
\newcommand*{\writestartgrid}{%
  \immediate\closeout\s@dwrite
  \immediate\openout\s@dwrite=\prevfile
  \immediate\write\s@dwrite{123456789}
  \immediate\write\s@dwrite{456789123}
  \immediate\write\s@dwrite{789123456}
  \immediate\write\s@dwrite{234567891}
  \immediate\write\s@dwrite{567891234}
  \immediate\write\s@dwrite{891234567}
  \immediate\write\s@dwrite{345678912}
  \immediate\write\s@dwrite{678912345}
  \immediate\write\s@dwrite{912345678}
  \immediate\closeout\s@dwrite}

\newcommand*{\swaps}{\bgroup
  \setrannum{\maxrangectr}{36}{48}%
\gencommentary{number of swaps (36--48) = maxrangectr = \the\maxrangectr}
  \s@dtemploopcnta\@ne
  \loop
    \ifnum\s@dtemploopcnta<\maxrangectr
    \setrannum{\s@dtempcntb}{1}{18}%
%%\typeout{\space\space\space swap no \the\s@dtemploopcnta\space is \the\s@dtempcntb}
    \ifcase\s@dtempcntb
    \or \swapcolpair{1}{2}%
    \or \swapcolpair{1}{3}%
    \or \swapcolpair{2}{3}%
    \or \swapcolpair{4}{5}%
    \or \swapcolpair{4}{6}%
    \or \swapcolpair{5}{6}%
    \or \swapcolpair{7}{8}%
    \or \swapcolpair{7}{9}%
    \or \swapcolpair{8}{9}%
    \or \swaprowpair{1}{10}%
    \or \swaprowpair{1}{19}%
    \or \swaprowpair{10}{19}%
    \or \swaprowpair{28}{37}%
    \or \swaprowpair{28}{46}%
    \or \swaprowpair{37}{46}%
    \or \swaprowpair{55}{64}%
    \or \swaprowpair{55}{73}%
    \or \swaprowpair{64}{73}%
    \fi
    \advance\s@dtemploopcnta\@ne
  \repeat
\egroup}

%% swap the data in rows starting at #1, and #2
\newcommand*{\swaprowpair}[2]{\bgroup
  \s@dncol=#1\relax
  \s@dcolpos=#2\relax
  \s@dtemploopcnta\@ne
  \maxrangectr=10\relax
  \loop
    \ifnum\s@dtemploopcnta<\maxrangectr
    \s@dtempcnta=\useknt{s@lans\the\s@dncol}%
    \global\useknt{s@lans\the\s@dncol}=\useknt{s@lans\the\s@dcolpos}%
    \global\useknt{s@lans\the\s@dcolpos}=\s@dtempcnta
    \advance\s@dncol\@ne
    \advance\s@dcolpos\@ne
    \advance\s@dtemploopcnta\@ne
  \repeat
\egroup}

%% swap the data in cols starting #1 and #2
\newcommand*{\swapcolpair}[2]{\bgroup
  \s@dnrow=#1\relax
  \s@drowpos=#2\relax
  \s@dtemploopcnta\@ne
  \maxrangectr=10\relax
  \loop
    \ifnum\s@dtemploopcnta<\maxrangectr
    \s@dtempcnta=\useknt{s@lans\the\s@dnrow}%
    \global\useknt{s@lans\the\s@dnrow}=\useknt{s@lans\the\s@drowpos}%
    \global\useknt{s@lans\the\s@drowpos}=\s@dtempcnta
    \advance\s@dnrow 9\relax
    \advance\s@drowpos 9\relax
    \advance\s@dtemploopcnta\@ne
  \repeat
\egroup}

\newcommand*{\elimclues}{\bgroup
  \initialelimination
  \writestate{\prevfile}%
  \writestate{\currfile}%
  \elimcluesonebyone
  \getproblem{\prevfile}
  \writestate{\genfile}
\egroup}

\newcommand*{\elimseventeen}{\bgroup
  \s@dtemploopcnta\@ne
  \maxrangectr 18\relax
  \loop
    \ifnum\s@dtemploopcnta<\maxrangectr
    \setrannum{\s@dtempcnta}{1}{81}
    \global\useknt{s@lans\the\s@dtempcnta}=111111111\relax
    \advance\s@dtemploopcnta\@ne
  \repeat
\egroup}

%% eliminates all clues in column 5 and row 5
\newcommand*{\elimcross}{\bgroup
  \s@dtemploopcnta 5\relax
  \loop
    \ifnum\s@dtemploopcnta<78\relax%    78 = 5 + 73
    \global\useknt{s@lans\the\s@dtemploopcnta}=111111111\relax
    \advance\s@dtemploopcnta 9\relax
  \repeat
  \s@dtemploopcnta 37\relax
  \loop
    \ifnum\s@dtemploopcnta<46\relax%    46 = 37 + 9
    \global\useknt{s@lans\the\s@dtemploopcnta}=111111111\relax
    \advance\s@dtemploopcnta \@ne
  \repeat
\egroup}

\newcommand*{\elimnum}[1]{\bgroup
  \s@dtemploopcnta\@ne
  \loop
    \ifnum\s@dtemploopcnta<82\relax
    \ifnum\useknt{s@lans\the\s@dtemploopcnta}=-#1\relax
      \global\useknt{s@lans\the\s@dtemploopcnta}=111111111\relax
    \fi
    \advance\s@dtemploopcnta \@ne
  \repeat
\egroup}

\newcommand*{\elimcrossandnines}{\bgroup
  \elimcross
  \s@dtemploopcnta\@ne
  \loop
    \ifnum\s@dtemploopcnta<82\relax
    \ifnum\useknt{s@lans\the\s@dtemploopcnta}=-9\relax
      \global\useknt{s@lans\the\s@dtemploopcnta}=111111111\relax
    \fi
    \advance\s@dtemploopcnta \@ne
  \repeat
\egroup}

\newcommand*{\elimex}{\bgroup
  \s@dtemploopcnta\@ne
  \loop
    \ifnum\s@dtemploopcnta<82\relax
    \global\useknt{s@lans\the\s@dtemploopcnta}=111111111\relax
    \advance\s@dtemploopcnta 10\relax
  \repeat
  \s@dtemploopcnta=9\relax
  \loop
    \ifnum\s@dtemploopcnta<81\relax
    \global\useknt{s@lans\the\s@dtemploopcnta}=111111111\relax
    \advance\s@dtemploopcnta 8\relax
  \repeat
\egroup}

\newcommand*{\elimcrossandex}{\bgroup
  \elimcross
  \elimex
\egroup}

\newcommand*{\elimcrossandexandnines}{\bgroup
  \elimcross
  \elimex
  \elimnum{9}
\egroup}

\let\initialelimination\elimseventeen
\let\initialelimination\elimcross
\let\initialelimination\elimcrossandnines
\let\initialelimination\elimcrossandex
\let\initialelimination\elimcrossandexandnines

\newcount\toomanyloops
\newcommand*{\elimcluesonebyone}{\bgroup
%%%\typeout{***elimcluesonebyone}
\toomanyloops\z@
  \s@dtempcntb=81\relax
  \loop
    \ifnum\s@dtempcntb>80\relax
\advance\toomanyloops\@ne
    \getproblem{\currfile}
    \trysolution
    \ifnum\solcnt=81\relax     % can delete another clue
      \getproblem{\currfile}
      \writestate{\prevfile}
      \deleteaclue
      \writestate{\currfile}%
      \s@dtempcntb=81\relax
    \else % done
      \typeout{Solver failed, solcnt = \the\solcnt}
      \s@dtempcntb=\@ne
    \fi
    \ifnum\toomanyloops>80\relax
      \typeout{Too much looping}
      \s@dtempcntb=\@ne
    \fi
  \repeat
\egroup}

\newif\ifacluenotdeleted
\newcommand*{\deleteaclue}{\bgroup
  \acluenotdeletedtrue
  \loop
    \ifacluenotdeleted
    \setrannum{\s@dtempcnta}{1}{81}%
      \ifnum\useknt{s@lans\the\s@dtempcnta}<\z@%  found a clue
        \gencommentary{Deleting clue for cell \the\s@dtempcnta}%
        \global\useknt{s@lans\the\s@dtempcnta}=111111111\relax
        \acluenotdeletedfalse
      \fi
  \repeat
\egroup}

\newcommand*{\trysolution}{\bgroup
  \reduceallcells
  \keepreducing
\egroup}

%% \writestate{<outputfile>}
\newcommand*{\writestate}[1]{\bgroup
  \immediate\closeout\s@dwrite
  \immediate\openout\s@dwrite=#1
  \s@dnrow\@ne
  \loop
    \ifnum\s@dnrow<81\relax
    \gatherline{\the\s@dnrow}%
    \immediate\write\s@dwrite{\sudaline}%
    \advance\s@dnrow 9\relax
  \repeat
  \immediate\closeout\s@dwrite
\egroup}

\endinput
%%
%% End of file `createsudoku.sty'.
