% sudokupw.tex   

\begin{filecontents}{sudokupw.sty}

%% sudokupw.sty   Typesetting suduko puzzles
%% Author: Peter Wilson, all rights reserved

%% open game file
\newread\s@dread
\newcommand*{\reads@dgame}[1]{%
  \closein\s@dread
  \openin\s@dread=#1}

%% size of a game cell
\newlength\halfs@dcell
\newcommand*{\cellsize}[1]{%
  \halfs@dcell=#1\relax
  \halfs@dcell=0.5\halfs@dcell}
\cellsize{2.5\baselineskip}

%% font for the numbers
\newcommand*{\cluefont}[1]{\def\s@dfont{#1}}
\cluefont{\Huge}

%% counts for looping and cell positions
\newcount\s@dncol
\newcount\s@dnrow
\newcount\s@dcolpos
\newcount\s@drowpos

%% insert the clues into the game array
\newcommand*{\adds@dclues}{%
  \firsts@dcluetrue
  \s@dnrow\@ne
  \loop
    \ifnum\s@dnrow<10\relax
    \read\s@dread to \s@dline
    \firsts@dcluetrue
    \s@drowpos=\s@dnrow
    \multiply\s@drowpos\tw@
    \advance\s@drowpos\m@ne
    \s@dncol\@ne
    \dos@dcols
    \advance\s@dnrow\@ne
  \repeat
  \closein\s@dread}

%% insert a row of clues
\newcommand*{\dos@dcols}{%
\bgroup
  \loop
    \ifnum\s@dncol<10\relax
    \s@dcolpos=\s@dncol
    \multiply\s@dcolpos\tw@
    \advance\s@dcolpos\m@ne
    \put(\s@dcolpos,-\s@drowpos){\makebox(0,0){\s@dfont\gets@dclue}}%
    \advance\s@dncol\@ne
  \repeat
\egroup}
 
%% get the next character in a string
\def\gettwo#1#2\nowt{%
  \gdef\istchar{#1}\gdef\restchars{#2}}
\def\splitoff#1{\gettwo#1\nowt}

%% a full stop (period)
\gdef\s@dfstop{.}
    
%% get the next clue in the line
\newif\iffirsts@dclue
\firsts@dcluetrue
\newcommand*{\gets@dclue}{%     
  \iffirsts@dclue
    \expandafter\splitoff\expandafter{\s@dline}%
    \global\firsts@dcluefalse
  \else
    \expandafter\splitoff\expandafter{\restchars}%
  \fi
  \ifx\s@dfstop\istchar
  \else
    \istchar
  \fi}

%% typeset the array, then add the clues
\newcommand*{\s@dgame}{%
  \setlength\unitlength\halfs@dcell
  \begin{picture}(18,18)(0,-18)
    \thinlines
    \multiput(0,0)(2,0){10}{\line(0,-1){18}}
    \multiput(0,0)(0,-2){10}{\line(1,0){18}}
    \thicklines \linethickness{1pt}
    \multiput(0,0)(6,0){4}{\line(0,-1){18}}
    \multiput(0,0)(0,-6){4}{\line(1,0){18}}
    \adds@dclues
  \end{picture}}

%% the whole shebang, where #1 is the name of the game file
\newcommand*{\sudoku}[1]{%
  \reads@dgame{#1}%
  \s@dgame}

\endinput

\end{filecontents}

\begin{filecontents*}{practexpw.sud}
..483..72
.12....8.
..52.13..
....62.91
7..5.9..3
94.78....
..39.74..
.5....61.
28..469..

PracTeX Sudoku puzzle
\end{filecontents*}

\begin{filecontents*}{practexpw.ans}
694835172
312674589
875291364
538462791
726519843
941783256
163957428
459328617
287146935

PracTeX sudoku puzzle answer
\end{filecontents*}

\documentclass{article}
\usepackage{sudokupw}
\pagestyle{empty}
\title{PracTeX Journal 2005-4 Sudoku Puzzle}
\author{Peter Wilson}
%\date{29 December 2005}
\date{}
\addtolength{\textheight}{5\baselineskip}

\begin{document}
\maketitle
\thispagestyle{empty}

THE CHALLENGE
\begin{center}
\sudoku{practexpw.sud}
\end{center}

\clearpage
\cluefont{\normalsize}
\cellsize{1.5\baselineskip}
THE ANSWER
\begin{center}
\sudoku{practexpw.ans}
\end{center}

\end{document}
