2019/09/23 18:03、Alexander Ilin <ajs...@yandex.ru>のメール:
> 
> 
> Have you used the `backtrack` vocab for this?
> https://re-factor.blogspot.com/2015/06/send-more-money.html

I ported a Prolog program to solve the same puzzle.



USING: logica lists assocs sequences kernel math
locals formatting io ;
IN: logica.test.money

LOGIC-PREDS: sumo sum1o digitsumo delo nonzeroo donaldo moneyo ;
LOGIC-VARS: S E N D M O R Y A L G B T
            N1 N2 C C1 C2 D1 D2 L1
            Digits Digs Digs1 Digs2 Digs3 ;

{ sumo N1 N2 N } {
    { sum1o N1 N2 N 0 0 L{ 0 1 2 3 4 5 6 7 8 9 } __ }
} si

{ sum1o L{ } L{ } L{ } 0 0 Digits Digits } semper
{ sum1o [ D1 N1 cons ] [ D2 N2 cons ] [ D N cons ] C1 C Digs1 Digs } {
    { sum1o N1 N2 N C1 C2 Digs1 Digs2 }
    { digitsumo D1 D2 C2 D C Digs2 Digs }
} si

{ digitsumo D1 D2 C1 D C Digs1 Digs } {
    { delo D1 Digs1 Digs2 }
    { delo D2 Digs2 Digs3 }
    { delo D Digs3 Digs }
    [ [ [ D1 of ] [ D2 of ] [ C1 of ] tri + + ] S is ]
    [ [ S of 10 mod ] D is ]
    [ [ S of 10 / >integer ] C is ]
} si

{ delo A L L } { { nonvaro A } | } si
{ delo A [ A L cons ] L } semper
{ delo A [ B L cons ] [ B L1 cons ] } { delo A L L1 } si


{ moneyo L{ 0 S E N D }
         L{ 0 M O R E }
         L{ M O N E Y }
} semper

{ donaldo  L{ D O N A L D }
           L{ G E R A L D }
           L{ R O B E R T } 
} semper

:: S-and-M-can't-be-zero ( seq -- seq' )
    seq [| hash |
         1 hash N1 of list>array nth 0 = not
         1 hash N2 of list>array nth 0 = not and
    ] filter ;

:: print-puzzle ( hash-array -- )
    hash-array
    [| hash |
     "   " printf hash N1 of list>array [ "%d " printf ] each nl
     "+  " printf hash N2 of list>array [ "%d " printf ] each nl
     "----------------" printf nl
     "   " printf hash N  of list>array [ "%d " printf ] each nl nl
    ] each ;




IN: scratchpad [ { { moneyo N1 N2 N } { sumo N1 N2 N } } query
  S-and-M-can't-be-zero
  print-puzzle
] time
   0 9 5 6 7 
+  0 1 0 8 5 
----------------
   1 0 6 5 2 

Running time: 0.320353468 seconds


--
KUSUMOTO Norio



_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to