Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Mumps > Re: Puzzle - Co...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 913 of 992
Post > Topic >>

Re: Puzzle - Constraint Logic Programming

by Ed de Moel <demoel@[EMAIL PROTECTED] > Dec 7, 2007 at 05:17 PM

Indeed!

I ran the little program shown below.
With a "standard brute force" approach, it took 31 seconds (type=1).
With the extra constraints it took only 22 seconds (type=2).

EDM>d puzzle^permut(1)

526485+197485=723970
That took 31 seconds.
EDM>d puzzle^permut(2)

526485+197485=723970
That took 22 seconds.
EDM>

(Try do test^permut to see what the routine actually does...)


permut ; Ed de Moel ; permutations
  q
  ;
do(head,tail) New i
  If $Length(tail)=1 Do @[EMAIL PROTECTED]
 Quit
  For i=1:1:$Length(tail) Do 
do(head_$Extract(tail,i),$Extract(tail,1,i-1)_$Extract(tail,i+1,$Length(tail)))
  Quit
  ;
test ;
  Set action="act^permut(head_tail)"
  Do do("","abc")
  Quit
  ;
act(txt) Write !,txt
  Quit
  ;
puzzle(type) ; To solve the puzzle:
  New action,e,h1,h2,i,list,x
  ;
  ;
  Set h1=$Horolog,h1=h1*86400+$Piece(h1,",",2)
  ;    D O N A L D
  ;  + G E R A L D
  ;  -------------
  ;    R O B E R T
  ;
  ; First recognize the different letters:
  ;      123456789-123456789-
  Set x="DONALD+GERALD=ROBERT"
  Kill list
  For i=1:1:$Length(x) Set e=$Extract(x,i) Set:e?1u list(e,i)=""
  Set i="",list="" For  Set i=$Order(list(i)) Quit:i=""  Set list=list_i
  ;
  Set:type=1 action="solve1^permut(head_tail)"
  Set:type=2 action="solve2^permut(head_tail)"
  Do do^permut("",1234567890)
  Set h2=$Horolog,h2=h2*86400+$Piece(h2,",",2)
  Write !,"That took ",h2-h1," seconds."
  Quit
  ;
solve1(text) New e,i,p
  Set e=$Translate(x,list,text) Xecute "If "_e_" Write !,e"
  Quit
  ;
solve2(text) New e,i,p
  Set e=$Translate(x,list,text)
  ; D in Donald may not be 0
  Quit:'$Extract(e,1)
  ; G in Gerald may not be 0
  Quit:'$Extract(e,8)
  ; T at the end must be even
  Quit:13579[$Extract(e,20)
  Xecute "If "_e_" Write !,e"
  Quit
  ;



-- 
**************************************************************
This electronic mail transmission contains confidential and/or
privileged information intended only for the person(s) named.
Any use, distribution, copying or disclosure by another person
is strictly prohibited.
**************************************************************
 




 2 Posts in Topic:
Puzzle - Constraint Logic Programming
Steve Graham <solitary  2007-12-06 08:12:36 
Re: Puzzle - Constraint Logic Programming
Ed de Moel <demoel@[EM  2007-12-07 17:17:43 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 18:23:29 CDT 2008.