Enigmatic Code

Programming Enigma Puzzles

Puzzle 71: All wrong, all wrong

From New Scientist #1122, 28th September 1978 [link]

A couple of one’s, a couple of two’s and a six;
All wrong, all wrong!

If only I thought that the puzzle was one I could fix,
I’d sing a song.

But as I feel sure that it’s rather too much for me,
My voice is muted.

Uncle Bungle’s my name and I fear that you must agree,
I’m rather stupid.

So please, I implore,
Continue the fight,
With tooth and with claw,
With main and with might,
To make wrong sums right.

The figures given are all incorrect. Write out the whole division sum.

[puzzle71]

Advertisements

3 responses to “Puzzle 71: All wrong, all wrong”

1. Jim Randell 21 June 2017 at 4:11 pm

Here’s a solution that uses the general alphametic solver (SubstitutedExpression()) from the enigma.py library.

We represent the parts of the diagram as a number of alphametic expressions, and restrict the letters that have digits given from taken on those values.

This run file executes in 103ms.

```#!/usr/bin/env python -m enigma -r

# consider the division:
#
#          G H I
#      ---------
#  E F ) A B C D
#        J K
#        ---
#        L M C
#        N O P
#        -----
#            Q D
#            Q D
#            ===

# solver to use
SubstitutedExpression

# solver parameters
--distinct=""
--invalid="0,AEGJLNQ"
--invalid="1,GL"
--invalid="2,EN"
--invalid="6,J"

# the main division sum
"GHI * EF = ABCD"

# the multiples
"G * EF = JK"
"H * EF = NOP"
"I * EF = QD"

# the intermediate subtraction sums
"AB - JK = LM"
"LMC - NOP = Q"
```

Solution: The correct sum is: 9828 ÷ 39 = 252.

Since Enigma 389 I’ve been thinking about replacing the SubstitutedDivision() solver (originally written for Enigma 206, but used to provide solutions for many other substituted division problems – until I wrote the SubstitutedExpression() solver) with a solver that will turn a long division problem into a set of expressions suitable for feeding to the SubstitutedExpression() solver. I’ve generally found that using SubstitutedExpression() produces code that executes faster than the SubstitutedDivision() solver, and using the SubstitutedExpression() solver has a lot more bells and whistles than the SubstitutedDivision() solver.

I currently have the new SubstitutedDivision() solver in testing and will roll it out to enigma.py shortly. While it will handle most SubstitutedDivision() problems in the same way, there are some cases where a different approach is used with the new solver. Once the new solver is launched I will check my solutions I have previously posted using the old solver and add revised code if necessary.

• Jim Randell 15 July 2017 at 12:10 pm

The new implementation of the SubstitutedDivision() solver is now available in the enigma.py library (version 2017-06-21). So this puzzle can be solved by the following run file. It executes in 118ms.

```#!/usr/bin/env python -m enigma -r
#
# consider the division:
#
#          B ? ?
#      ---------
#  A ? ) ? ? ? ?
#        C ?
#        ---
#        D ? ?
#        E ? ?
#        -----
#            ? ?
#            ? ?
#            ===

# solver to use
SubstitutedDivision

# solver parameters
--distinct=""
--invalid="1,BD"
--invalid="2,AE"
--invalid="6,C"

# the main division sum
"???? / A? = B??"

# the intermediate subtraction sums
"?? - C? = D?"
"D?? - E?? = ?"
"?? - ?? = 0"
```
2. geoffrounce 22 June 2017 at 11:11 am

I re-used your your letter description of the division puzzle and your solver details to code a full programme solution with a written division sum at the end of the code.

```% A Solution in MiniZinc
include "globals.mzn";

%          G H I
%      ---------
%  E F ) A B C D
%        J K
%        ---
%        L M C
%        N O P
%        -----
%            Q D
%            Q D
%            ===

var 0..9:A;  var 0..9:B;  var 0..9:C;
var 0..9:D;  var 0..9:E;  var 0..9:F;
var 0..9:G;  var 0..9:H;  var 0..9:I;
var 0..9:J;  var 0..9:K;  var 0..9:L;
var 0..9:M;  var 0..9:N;  var 0..9:O;
var 0..9:P;  var 0..9:Q;

% Digits which cannot be zero
constraint A != 0 /\ E != 0 /\ G != 0 /\ J != 0
/\ L != 0 /\ N != 0 /\ Q != 0;

% Invalid digits given in the puzzle
constraint G != 1 /\ L != 1 /\ E != 2 /\ N != 2 /\ J != 6;

% Variables used in the division puzzle
var 10..99: EF = 10*E + F;
var 10..99: JK = 10*J + K;
var 10..99: QD = 10*Q + D;
var 10..99: LM = 10*L + M;
var 10..99: AB = 10*A + B;
var 100..999: GHI = 100*G + 10*H + I;
var 100..999: LMC = 100*L + 10*M + C;
var 100..999: NOP = 100*N + 10*O + P;
var 1000..9999: ABCD = 1000*A + 100*B + 10*C + D;

% the main division sum
constraint GHI * EF == ABCD;

% multiplication constraints
constraint G * EF == JK /\ H * EF == NOP /\ I * EF == QD;

% intermediate subtraction sum constraints
constraint AB - JK == LM /\ LMC - NOP == Q;

solve satisfy;

output [ show(ABCD) ++ " / " ++ show(EF) ++ " = " ++ show(GHI)  ++ "\n" ++
"JK = " ++ show(JK) ++ " LMC = " ++ show(LMC) ++ " NOP = " ++ show(NOP) ++
" NOP = " ++ show(NOP) ++ " QD = " ++ show(QD) ];

% 9828 / 39 = 252
% JK = 78 LMC = 202 NOP = 195 NOP = 195 QD = 78
% Finished in 133msec
%
%        252
%     ------
%   39) 9828
%       78
%      ---
%       202
%       195
%       ---
%         78
%         78
%         ==

```