Cdok

Daniel Beer

24 May 2013

Calcudoku is a sudoku-like game invented by Tetsuya Miyamoto (marketed under the trademarked name "KenKen"). The following is an example of a 6x6 calcudoku puzzle:

╔═════╦═════╦═════╦═════╦═══════════╗
║12+  ║4/   ║     ║     ║3*   ┊     ║
║     ║     ║  5  ║  2  ║     ┊     ║
║     ║     ║     ║     ║     ┊     ║
║┈┈┈┈┈║┈┈┈┈┈╠═════╩═════╬═══════════╣
║     ║     ║7+   ┊     ║60*  ┊     ║
║     ║     ║     ┊     ║     ┊     ║
║     ║     ║     ┊     ║     ┊     ║
║┈┈┈┈┈╠═════╬═══════════╩═════╗┈┈┈┈┈║
║     ║12+  ║28+  ┊     ┊     ║     ║
║     ║     ║     ┊     ┊     ║     ║
║     ║     ║     ┊     ┊     ║     ║
╠═════╣┈┈┈┈┈╠═══════════╗┈┈┈┈┈╚═════╣
║     ║     ║1-   ┊     ║     ┊     ║
║  3  ║     ║     ┊     ║     ┊     ║
║     ║     ║     ┊     ║     ┊     ║
╠═════╣┈┈┈┈┈╚═════╦═════╣┈┈┈┈┈╔═════╣
║     ║     ┊     ║11+  ║     ║2*   ║
║  4  ║     ┊     ║     ║     ║     ║
║     ║     ┊     ║     ║     ║     ║
╠═════╩═══════════╣┈┈┈┈┈╠═════╣┈┈┈┈┈║
║10+  ┊     ┊     ║     ║     ║     ║
║     ┊     ┊     ║     ║  4  ║     ║
║     ┊     ┊     ║     ║     ║     ║
╚═════════════════╩═════╩═════╩═════╝

As in sudoku, each row and each column must contain the numbers 1 to 6 once each. Some initial values may be given. There are no sub-boxes, but groups of connected cells outlined in double-lines contain a clue in the top-left corner (e.g. "11+"). The clue consists of a target value and one of the characters +, -, * or /, indicating the type of the group:

Sum: +

The numbers in this group must add to the target value.

Difference: -

One of the numbers in this group, minus the rest, must equal the target value (the number to be subtracted from may be any of the cells).

Product: *

The numbers in this group must multiply to produce the target value.

Ratio: /

One of the numbers in this group, divided by all of the others, must equal the target value. As in a difference group, which of the cells contains the number to be divided is not specified.

Numbers may be repeated within a group (so long as they're not also repeated within a row or column). Each puzzle has a unique solution. The solution to the example shown above is:

╔═════╦═════╦═════╦═════╦═══════════╗
║12+  ║4/   ║     ║     ║3*   ┊     ║
║  6  ║  4  ║  5  ║  2  ║  1  ┊  3  ║
║     ║     ║     ║     ║     ┊     ║
║┈┈┈┈┈║┈┈┈┈┈╠═════╩═════╬═══════════╣
║     ║     ║7+   ┊     ║60*  ┊     ║
║  5  ║  1  ║  4  ┊  3  ║  2  ┊  6  ║
║     ║     ║     ┊     ║     ┊     ║
║┈┈┈┈┈╠═════╬═══════════╩═════╗┈┈┈┈┈║
║     ║12+  ║28+  ┊     ┊     ║     ║
║  1  ║  2  ║  6  ┊  4  ┊  3  ║  5  ║
║     ║     ║     ┊     ┊     ║     ║
╠═════╣┈┈┈┈┈╠═══════════╗┈┈┈┈┈╚═════╣
║     ║     ║1-   ┊     ║     ┊     ║
║  3  ║  6  ║  2  ┊  1  ║  5  ┊  4  ║
║     ║     ║     ┊     ║     ┊     ║
╠═════╣┈┈┈┈┈╚═════╦═════╣┈┈┈┈┈╔═════╣
║     ║     ┊     ║11+  ║     ║2*   ║
║  4  ║  3  ┊  1  ║  5  ║  6  ║  2  ║
║     ║     ┊     ║     ║     ║     ║
╠═════╩═══════════╣┈┈┈┈┈╠═════╣┈┈┈┈┈║
║10+  ┊     ┊     ║     ║     ║     ║
║  2  ┊  5  ┊  3  ║  6  ║  4  ║  1  ║
║     ┊     ┊     ║     ║     ║     ║
╚═════════════════╩═════╩═════╩═════╝

This program, cdok, can be used to examine, print, solve and generate these puzzles. Puzzles are parsed in a "puzzle spec" format, where rows are given one per line, with cells separated by spaces. Groups are labelled with alphabetic characters, which are then used to identify cell membership. A valid puzzle spec for the example above is:

J+12    F/4     5       2       E*3     E
J       F       H+7     H       I*60    I
J       D+12    G+28    G       G       I
3       D       K-1     K       G       G
4       D       D       B+11    G       A*2
C+10    C       C       B       4       A

Puzzle specs may be terminated by a blank line. Anything following the blank line is ignored when parsing.

Installation

The source is hosted on github, and you can clone the repository with:

git clone git://github.com/dlbeer/cdok.git

If you don't have git installed, you can download a snapshot.

To build cdok, type make. Type make install to install the resulting binary.

Try it out by typing cdok generate to generate a 6x6 puzzle. If your terminal supports UTF-8, use the -u flag to draw the puzzle using UTF-8 line-drawing characters.

Some definitions of Calcudoku require that difference and ratio groups be limited to two cells (to avoid confusion due to the non-associativity of subtraction and division). Cdok does not impose this requirement in generated puzzles by default, but it will if you use the -T option.

Type cdok --help to get a full list of options.

Copyright (C) 2012 Daniel Beer

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.