1 ! Copyright (C) 2019-2020 KUSUMOTO Norio.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: logic lists assocs sequences kernel math
5 IN: logic.examples.money
7 LOGIC-PREDS: sumo sum1o digitsumo delo donaldo moneyo ;
8 LOGIC-VARS: S E N D M O R Y A L G B T
10 Digits Digs Digs1 Digs2 Digs3 ;
13 { sum1o N1 N2 N 0 0 L{ 0 1 2 3 4 5 6 7 8 9 } __ }
16 { sum1o L{ } L{ } L{ } 0 0 Digits Digits } fact
17 { sum1o L{ D1 . N1 } L{ D2 . N2 } L{ D . N } C1 C Digs1 Digs } {
18 { sum1o N1 N2 N C1 C2 Digs1 Digs2 }
19 { digitsumo D1 D2 C2 D C Digs2 Digs }
22 { digitsumo D1 D2 C1 D C Digs1 Digs } {
23 { delo D1 Digs1 Digs2 }
24 { delo D2 Digs2 Digs3 }
26 [ [ [ D1 of ] [ D2 of ] [ C1 of ] tri + + ] S is ]
27 [ [ S of 10 mod ] D is ]
28 [ [ S of 10 / >integer ] C is ]
31 { delo A L L } { { nonvaro A } !! } rule
32 { delo A L{ A . L } L } fact
33 { delo A L{ B . L } L{ B . L1 } } { delo A L L1 } rule
47 :: S-and-M-can't-be-zero ( seq -- seq' )
49 1 hash N1 of list>array nth 0 = not
50 1 hash N2 of list>array nth 0 = not and
53 :: print-puzzle ( hash-array -- )
56 " " printf hash N1 of list>array [ "%d " printf ] each nl
57 "+ " printf hash N2 of list>array [ "%d " printf ] each nl
58 "----------------" printf nl
59 " " printf hash N of list>array [ "%d " printf ] each nl nl