19. Sudoku: Validator (Part I.)
The mathematics lecture hall hummed with anticipation that afternoon. At the front stood a guest—Monsieur Delaroche, a visiting professor from Paris with a waxed moustache and the air of a man who delighted in novelties. He flourished a folded newspaper, its pages bearing the seal of Le Siècle.
„Gentlemen, and ladies too, if any brave enough attend,“ he began with a theatrical bow, „behold a riddle now popular in France. A puzzle not of sums or products, but of order, of harmony itself.“
He chalked a 9×9 grid on the board, filling a few cells with numbers. The pattern looked both arbitrary and strict, as if hiding a deeper cadence.
Johann, lounging in the back, whispered loudly enough for all to hear: „The French—first they trade kings for republics, now they trade poetry for riddles!“ The class chuckled, but Monsieur Delaroche pressed on, unbothered.
„Each number from 1 to 9 must appear once—no more, no less—in every row, every column, and each of the 9 smaller boxes. Fill them rightly, and the picture is complete. Fail, and the whole edifice collapses. A true test of rigor!“
Mihkel leaned forward, pulse quickening. This was unlike anything he had faced before. It was not a single computation, but a symphony of conditions, each demanding satisfaction. To solve such a riddle was daunting—but to verify whether a board was sound? That was possible. One part at a time. Validator first, solver later.
That evening, in the narrow workshop beside his quarters, Mihkel unrolled a strip of tape marked with the grid. The Logic Mill stirred as he adjusted its cranks. Gears clattered faintly in the lamplight, carrying his instructions into motion: check each row, each column, each box—one after another, as relentless as truth itself.
But he was not alone. Outside, in the corridor’s gloom, Professor Reichenstein lingered, having followed Mihkel after lecture. He kept to the shadows, silent, his ear near the door. From within came faint ticks and whirrs—mechanical, regular, unlike quill or abacus.
A floorboard groaned. Mihkel’s hand froze on the crank. He snuffed the lamp, slid the Mill beneath a canvas sheet, and sat motionless, breath shallow.
The noise outside ceased. Reichenstein, wary of revealing himself, withdrew without knocking. Yet the suspicion in his heart only deepened.
On the input tape, you’ll get a 9×9 Sudoku board in a single line, with rows separated by =
and groups of 3 cells in a row separated by |
.
Empty cells are represented by 0
.
Your task is to check whether the board is valid and output Y
for a valid and N
for an invalid board.
Only the filled (non-zero) cells need to be checked.
The board should be validated according to the Sudoku rules:
Each number from 1 to 9 can appear at most once in
- each row
- each column
- each of the nine 3×3 boxes of the grid
For example, if the input tape is
143|657|028= 682|314|579= 571|289|346= 726|493|851= 315|862|497= 894|571|263= 457|136|982= 068|925|734= 239|748|615
your output tape should be N
, because the board is invalid (the number 1 appears twice in the top-left 3×3 box).
(for the sake of readability, I added new lines to the example; the actual input tape doesn't have them)