Solving the Puzzle

Solving the Can You Solve The Impossible Puzzle? with Common Lisp and Org Mode.

Our search space contains 45 possible candidates of pairs of numbers. The tables below will have one candidate of a pair on each row. In the first column, we have the multiple, in the second column the sum and the last two columns the candidate pair. The prune function will help us to reduce the search space on each interaction, filtering the elements without repetition.

(ql:quickload :group-by)

(defun prune (data func)
  (let* ((pre (group-by:group-by-repeated data :keys (list func)))
         (input (remove-if (lambda (p) (<= (length (cdr p)) 1)) pre)))
    (reduce (lambda (acc a) (append acc (cdr a))) input
            :initial-value '((m s x y)))))

(cons '(m s x y)
      (loop for x from 1 to 9
            append (loop for y from 9 downto x
                         collect (list (* x y) (+ x y) x y))))

#+RESULTS[ca79f9a22ba8cd356c32197a5ac0ad93f8159c81]: start

msxy
91019
8918
7817
6716
5615
4514
3413
2312
1211
181129
161028
14927
12826
10725
8624
6523
4422
271239
241138
211037
18936
15835
12734
9633
361349
321248
281147
241046
20945
16844
451459
401358
351257
301156
251055
541569
481468
421367
361266
631679
561578
491477
721789
641688
811899

In the first time that Barack asked Pete, if Pete knew the answer his multiple would be unique defined in the candidate list, that was not the case, so we must remove the multiples without repetitions.

(prune (cdr data) #'first)

#+RESULTS[6db81e23c88ea3483e1865437f8de0f9cca170cd]: step-1

msxy
361266
361349
241046
241138
12734
12826
16844
161028
18936
181129
4422
4514
6523
6716
8624
8918
9633
91019

When Barack asked Susan for the first time, she already knew that Pete didn’t know the answer either. So the candidate list in her mind is the list above. But she didn’t know the answer of Barack’s question either, so her sum are not unique in this list too.

(prune (cdr data) #'second)

#+RESULTS[2ac1f2a3d955fbf7a89f6db99a91c8f902775483]: step-2

msxy
9633
8624
6523
4514
8918
18936
16844
12826
6716
12734
181129
241138
91019
161028
241046

In the second time that Barack asked Pete, he still didn’t know. So we have to exclude all unique multiples again.

(prune (cdr data) #'first)

#+RESULTS[d5b928145f97d2bea7471b63383a9e34d6178b5a]: step-3

msxy
241046
241138
12734
12826
161028
16844
181129
18936
6716
6523
8918
8624
91019
9633

The same again for the second time Barack asked Susan:

(prune (cdr data) #'second)

#+RESULTS[17b25e5fc689d147eda2bd35c388cde44f310568]: step-4

msxy
9633
8624
8918
18936
16844
12826
6716
12734
181129
241138
91019
161028
241046

Pete in the third time still didn’t know.

(prune (cdr data) #'first)

#+RESULTS[44d455fea1e59e9db1788bb012f6cdd4abcc32f1]: step-5

msxy
241046
241138
12734
12826
161028
16844
181129
18936
8918
8624
91019
9633

Susan in the third still didn’t know.

(prune (cdr data) #'second)

#+RESULTS[7a0a13546e37c1bd0d1fff1059eb069b381cbd30]: step-6

msxy
9633
8624
8918
18936
16844
12826
181129
241138
91019
161028
241046

Pete once more didn’t know:

(prune (cdr data) #'first)

#+RESULTS[079394364b443353af7d9353a8c0a38835b09ee2]: step-7

msxy
241046
241138
161028
16844
181129
18936
8918
8624
91019
9633

Susan in the fourth time didn’t know either:

(prune (cdr data) #'second)

#+RESULTS[e8f43474732654a4d80a659c50fe51b7ddba6a28]: step-8

msxy
9633
8624
8918
18936
181129
241138
91019
161028
241046

At this moment, in the fifth time, Pete knew the answer. That is, his number should be 16, since this is the only multiple that unique defines the candidates: 2 and 8.

If Pete didn’t knew at this time, Barack would have asked once more to Susan and we would have to exclude the pair (2,8) from the list of candidates:

(prune (cdr data) #'first)

#+RESULTS[b04f949aa4cfaea9ae9f63533fc50b207336f698]: step-9

msxy
241046
241138
181129
18936
8918
8624
91019
9633

In this candidate list, Susan would not be able to identify the numbers since no sum is unique.

Links