RAKEV

2024-11-20 perl Algorithm::Combinatorics riddle

Playing with my kids we found a riddle book and one of the tasks caugtht our attention. The assignment looks like this:

What are the numbers?

 LORD
 KORD
 MORD
-----
RAKEV

The riddle is in czech, the words together make a simple tragic story:

  • Lord = Lord
  • Kord = Dueling sword
  • Mord = Murder
  • Rakev = Coffin

The goal is to find a digit 0-9 for each letter involved.

As I was quite lazy, I decided to use simple program to find the solution (all solutions really) for me. This is what I came with, using Algorithm::Combinatorics module to produce all variations for all 9 letters in the riddle:

use 5.16.0;
use Algorithm::Combinatorics qw(variations);

my $found = 0;
my $it = variations([0..9], 9);
while(my $set = $it->next) {
    my ($D, $R, $O, $L, $K, $M, $V, $E, $A) = @$set;
    print(" $L$O$R$D\n $K$O$R$D\n $M$O$R$D\n-----\n$R$A$K$E$V\n\n") && $found++
        if     $L > 0
            && $K > 0
            && $M > 0
            && $R > 0
            &&                1000*($L+$K+$M) + 300*$O + 30*$R + 3*$D
                == 10000*$R + 1000*$A         + 100*$K + 10*$E +   $V;
}
say "Found: $found solutions";

In less than 5 seconds it provided following results:

 5821
 4821
 9821
-----
20463

 9821
 4821
 5821
-----
20463

 4912
 7912
 5912
-----
18736

 5912
 7912
 4912
-----
18736

 5327
 9327
 6327
-----
20981

 6327
 9327
 5327
-----
20981

 2318
 9318
 6318
-----
17954

 6318
 9318
 2318
-----
17954

 2619
 8619
 3619
-----
14857

 3619
 8619
 2619
-----
14857

Found: 10 solutions