22 May 2025, 8:41 p.m.

ST Code Breaker Puzzle in Prolog

Since it was a pretty easy puzzle this week, I thought I'd have a go a cracking the Sunday Telegraph "Code Breaker" using Prolog. Prolog is kind of a unique language, one which I studied in the AI labs at Edinburgh about 30 years ago, but on today's evidence, barely know! But we got there. Fairly self-explanatory if you have the puzzle in front of you.

:- use_module(library(clpfd)).

% I only buy the ST for the puzzles, trust me

solution(A, B, C, D) :-
    Vs = [A, B, C, D],

    Vs ins 1..9,
    all_distinct(Vs),

    D #= A + 3,
    xor(B mod 2 #= 1, C mod 2 #= 1),
    D #< 7,
    B #> A,
    D #> (B + C),
    label(Vs).

xor(X, Y) :-
    X #/\ #\Y #\/ #\X #/\ Y.

% ?- solution(A, B, C, D).
% A = 3,
% B = 4,
% C = 1,
% D = 6.

This was the puzzle for the 13th April, 2025. As I say, most weeks it's a fair bit harder, so I'll tackle a couple more in due course. And I clarify: I only ever bought the grim Sunday Telegraph for the puzzles. Oh, and its broadsheet size makes it ideal for putting under the cat's litter tray.

Read more »
Posted by Simon in Prolog | 0 comments