-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendmoremoney.lisp
executable file
·251 lines (227 loc) · 6.79 KB
/
sendmoremoney.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
(in-package :cl-user)
;;; Send
;;;+ More
;;;=Money
;;;To get a solution, try (test-sendMoreMoney)
;;;Run tests at least twice to get clos prepared
;;;Value Ordering is important, we get a solution between 23 833 and 11 380 457 tries
#-fast
(eval-when
#-:gcl (:compile-toplevel :execute :load-toplevel)
#+:gcl (compile eval load)
(proclaim '(optimize (speed 0) (safety 3) (space 0)(debug 3)(compilation-speed 0)))
)
#+fast
(eval-when
#-:gcl (:compile-toplevel :execute :load-toplevel)
#+:gcl (compile eval load)
(proclaim '(optimize (speed 3) (safety 0) (space 0)(debug 0)(compilation-speed 0)))
)
(defclass send-money-problem-carries (NUMERIC-SEVERAL-DOMAINS-PROBLEM-SPECIFICATION)
()
)
(defparameter *send-more-money-domains*
'((:s (0 1 2 3 4 5 6 7 8 9))
(:m (0 1 2 3 4 5 6 7 8 9))
(:o (0 1 2 3 4 5 6 7 8 9))
(:c-sm (0 1))
(:e (0 1 2 3 4 5 6 7 8 9))
(:n (0 1 2 3 4 5 6 7 8 9))
(:c-eo (0 1))
(:r (0 1 2 3 4 5 6 7 8 9))
(:c-nr (0 1))
(:d (0 1 2 3 4 5 6 7 8 9))
(:y (0 1 2 3 4 5 6 7 8 9))
(:c-de (0 1))
)
#+no
'(
(:d (0 1 2 3 4 5 6 7 8 9))
(:e (0 1 2 3 4 5 6 7 8 9))
(:y (0 1 2 3 4 5 6 7 8 9))
(:c-de (0 1))
(:n (0 1 2 3 4 5 6 7 8 9))
(:r (0 1 2 3 4 5 6 7 8 9))
(:c-nr (0 1))
(:o (0 1 2 3 4 5 6 7 8 9))
(:c-eo (0 1))
(:s (0 1 2 3 4 5 6 7 8 9))
(:m (0 1 2 3 4 5 6 7 8 9))
(:c-sm (0 1))
)
#+no
'(
(:c-sm (0 1))
(:c-eo (0 1))
(:c-nr (0 1))
(:c-de (0 1))
(:s (0 1 2 3 4 5 6 7 8 9))
(:e (0 1 2 3 4 5 6 7 8 9))
(:n (0 1 2 3 4 5 6 7 8 9))
(:d (0 1 2 3 4 5 6 7 8 9))
(:m (0 1 2 3 4 5 6 7 8 9))
(:o (0 1 2 3 4 5 6 7 8 9))
(:r (0 1 2 3 4 5 6 7 8 9))
(:y (0 1 2 3 4 5 6 7 8 9))
)
#+no
'(
(:s (0 1 2 3 4 5 6 7 8 9))
(:e (0 1 2 3 4 5 6 7 8 9))
(:n (0 1 2 3 4 5 6 7 8 9))
(:d (0 1 2 3 4 5 6 7 8 9))
(:m (0 1 2 3 4 5 6 7 8 9))
(:o (0 1 2 3 4 5 6 7 8 9))
(:r (0 1 2 3 4 5 6 7 8 9))
(:y (0 1 2 3 4 5 6 7 8 9))
(:c-sm (0 1))
(:c-eo (0 1))
(:c-nr (0 1))
(:c-de (0 1))
)
)
(defmethod all-domains-extended ((me send-money-problem-carries))
*send-more-money-domains*
)
(defmethod initialize-instance :after ((me send-money-problem-carries) &rest initargs)
(declare (ignore initargs))
(setf (my-constraints me)
(list
(make-instance 'index<>value
:sm-index (FIND-VARIABLE-INDEX me :s)
:sm-value 0)
(make-instance 'index<>value
:sm-index (FIND-VARIABLE-INDEX me :m)
:sm-value 0)
(make-instance 'value=value
:index-a (FIND-VARIABLE-INDEX me :m)
:index-b (FIND-VARIABLE-INDEX me :c-sm)
)
(make-instance 'all-distinct
:sm-indices (FIND-VARIABLE-INDices me '(:s :e :n :d :m :o :r :y)))
(make-instance 'COLUMN=
:value-a (FIND-VARIABLE-INDEX me :d)
:value-b (FIND-VARIABLE-INDEX me :e)
:mod10sum (FIND-VARIABLE-INDEX me :y)
:div10 (FIND-VARIABLE-INDEX me :c-de))
(make-instance 'COLUMN=CARRY
:value-a (FIND-VARIABLE-INDEX me :n)
:value-b (FIND-VARIABLE-INDEX me :r)
:mod10sum (FIND-VARIABLE-INDEX me :e)
:div10right (FIND-VARIABLE-INDEX me :c-de)
:div10 (FIND-VARIABLE-INDEX me :c-nr))
(make-instance 'COLUMN=CARRY
:value-a (FIND-VARIABLE-INDEX me :e)
:value-b (FIND-VARIABLE-INDEX me :o)
:mod10sum (FIND-VARIABLE-INDEX me :n)
:div10right (FIND-VARIABLE-INDEX me :c-nr)
:div10 (FIND-VARIABLE-INDEX me :c-eo))
(make-instance 'COLUMN=CARRY
:value-a (FIND-VARIABLE-INDEX me :s)
:value-b (FIND-VARIABLE-INDEX me :m)
:mod10sum (FIND-VARIABLE-INDEX me :o)
:div10right (FIND-VARIABLE-INDEX me :c-eo)
:div10 (FIND-VARIABLE-INDEX me :c-sm))
)))
(defclass sendMoreMoney-solver-backtracking-carries (NUMERIC-PUZZLE-SOLVER)
()
(:default-initargs
:specification (make-instance 'send-money-problem-carries)))
(defun test-sendMoreMoney (&optional (print t))
(test-backtracking "Send more Money Carries Backtracking" (make-instance 'sendMoreMoney-solver-backtracking-carries) print)
)
#|
(test-sendMoreMoney)
CG-USER(23):
; cpu time (non-gc) 550 msec user, 0 msec system
; cpu time (gc) 0 msec user, 0 msec system
; cpu time (total) 550 msec user, 0 msec system
; real time 550 msec
; space allocation:
; 862 cons cells, 5,056 other bytes, 0 static bytes
The solution in 23833 tries is:
Variable S is:9
Variable M is:1
Variable O is:0
Variable C-SM is:1
Variable E is:5
Variable N is:6
Variable C-EO is:0
Variable R is:8
Variable C-NR is:1
Variable D is:7
Variable Y is:2
Variable C-DE is:1
CG-USER(24):
; cpu time (non-gc) 930 msec user, 0 msec system
; cpu time (gc) 0 msec user, 0 msec system
; cpu time (total) 930 msec user, 0 msec system
; real time 930 msec
; space allocation:
; 20 cons cells, 248 other bytes, 0 static bytes
The solution in 45761 tries is:
Variable D is:7
Variable E is:5
Variable Y is:2
Variable C-DE is:1
Variable N is:6
Variable R is:8
Variable C-NR is:1
Variable O is:0
Variable C-EO is:0
Variable S is:9
Variable M is:1
Variable C-SM is:1
CG-USER(25):
; cpu time (non-gc) 41,470 msec user, 0 msec system
; cpu time (gc) 0 msec user, 0 msec system
; cpu time (total) 41,470 msec user, 0 msec system
; real time 41,470 msec
; space allocation:
; 176 cons cells, 408 other bytes, 560 static bytes
The solution in 5172656 tries is:
Variable C-SM is:1
Variable C-EO is:0
Variable C-NR is:1
Variable C-DE is:1
Variable S is:9
Variable E is:5
Variable N is:6
Variable D is:7
Variable M is:1
Variable O is:0
Variable R is:8
Variable Y is:2
CG-USER(26):
; cpu time (non-gc) 236,290 msec (00:03:56.290) user, 0 msec system
; cpu time (gc) 50 msec user, 0 msec system
; cpu time (total) 236,340 msec (00:03:56.340) user, 0 msec system
; real time 236,340 msec (00:03:56.340)
; space allocation:
; 922 cons cells, 648 other bytes, 560 static bytes
The solution in 11380457 tries is:
Variable S is:9
Variable E is:5
Variable N is:6
Variable D is:7
Variable M is:1
Variable O is:0
Variable R is:8
Variable Y is:2
Variable C-SM is:1
Variable C-EO is:0
Variable C-NR is:1
Variable C-DE is:1
|#
(defclass sendMoreMoney-solver-gsat-all-values (GSAT-ALL-VALUES-SOLVER)
()
(:default-initargs
:specification (make-instance 'send-money-problem-carries)))
(defun test-gsat-sendMoreMoney (&optional (print t))
(TEST-GSAT "Send more Money Carries Gsat all values" (make-instance 'SENDMOREMONEY-SOLVER-GSAT-ALL-VALUES :MAX-FLIPS 500 :max-tries 50) print)
)
#|
(TEST-GSAT-SENDMOREMONEY)
(dotimes (x 10)
(TEST-GSAT-SENDMOREMONEY))
|#