| 
									
										
										
										
											2008-07-16 02:03:27 -04:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: backtrack shuffle math math.ranges quotations locals fry | 
					
						
							| 
									
										
										
										
											2009-05-15 00:23:06 -04:00
										 |  |  | kernel words io memoize macros prettyprint sequences assocs | 
					
						
							| 
									
										
										
										
											2008-07-16 02:03:27 -04:00
										 |  |  | combinators namespaces ;
 | 
					
						
							|  |  |  | IN: benchmark.backtrack | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! This was suggested by Dr_Ford. Compute the number of quadruples | 
					
						
							|  |  |  | ! (a,b,c,d) with 1 <= a,b,c,d <= 10 such that we can make 24 by | 
					
						
							|  |  |  | ! placing them on the stack, and applying the operations | 
					
						
							|  |  |  | ! +, -, * and rot as many times as we wish. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-23 22:40:17 -05:00
										 |  |  | : nop ( -- ) ;
 | 
					
						
							| 
									
										
										
										
											2008-07-16 02:03:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : do-something ( a b -- c )
 | 
					
						
							|  |  |  |     { + - * } amb-execute ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : some-rots ( a b c -- a b c )
 | 
					
						
							| 
									
										
										
										
											2015-09-08 19:15:10 -04:00
										 |  |  |     ! Try to rot 0, 1 or 2 times. | 
					
						
							| 
									
										
										
										
											2008-07-16 02:03:27 -04:00
										 |  |  |     { nop rot -rot } amb-execute ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MEMO: 24-from-1 ( a -- ? )
 | 
					
						
							|  |  |  |     24 = ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MEMO: 24-from-2 ( a b -- ? )
 | 
					
						
							|  |  |  |     [ do-something 24-from-1 ] [ 2drop ] if-amb ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MEMO: 24-from-3 ( a b c -- ? )
 | 
					
						
							|  |  |  |     [ some-rots do-something 24-from-2 ] [ 3drop ] if-amb ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MEMO: 24-from-4 ( a b c d -- ? )
 | 
					
						
							|  |  |  |     [ some-rots do-something 24-from-3 ] [ 4drop ] if-amb ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : find-impossible-24 ( -- n )
 | 
					
						
							| 
									
										
										
										
											2015-06-01 22:46:40 -04:00
										 |  |  |     10 [1,b] [| a | | 
					
						
							|  |  |  |         10 [1,b] [| b | | 
					
						
							|  |  |  |             10 [1,b] [| c | | 
					
						
							|  |  |  |                 10 [1,b] [| d | | 
					
						
							| 
									
										
										
										
											2008-07-16 02:03:27 -04:00
										 |  |  |                     a b c d 24-from-4 | 
					
						
							|  |  |  |                 ] count
 | 
					
						
							| 
									
										
										
										
											2009-10-29 15:34:04 -04:00
										 |  |  |             ] map-sum
 | 
					
						
							|  |  |  |         ] map-sum
 | 
					
						
							|  |  |  |     ] map-sum ;
 | 
					
						
							| 
									
										
										
										
											2008-07-16 02:03:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-23 22:40:17 -05:00
										 |  |  | CONSTANT: words { 24-from-1 24-from-2 24-from-3 24-from-4 } | 
					
						
							| 
									
										
										
										
											2008-07-16 02:03:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : backtrack-benchmark ( -- )
 | 
					
						
							|  |  |  |     words [ reset-memoized ] each
 | 
					
						
							| 
									
										
										
										
											2012-07-19 22:10:09 -04:00
										 |  |  |     find-impossible-24 6479 assert=
 | 
					
						
							|  |  |  |     words [ "memoize" word-prop assoc-size ] map
 | 
					
						
							|  |  |  |     { 1588 5137 4995 10000 } assert= ;
 | 
					
						
							| 
									
										
										
										
											2008-07-16 16:13:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MAIN: backtrack-benchmark |