2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								USING: accessors arrays combinators.short-circuit grouping kernel lists
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								lists.lazy locals math math.functions math.parser math.ranges
							 | 
						
					
						
							
								
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								models.product monads random sequences sets ui ui.gadgets.controls
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								ui.gadgets.layout models.combinators ui.gadgets.alerts vectors fry
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-30 18:11:45 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								ui.gadgets.labels shuffle ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								IN: sudokus
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: row ( index -- row ) 1 + 9 / ceiling ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: col ( index -- col ) 9 mod 1 + ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: sq ( index -- square ) [ row ] [ col ] bi [ 3 / ceiling ] bi@ 2array ;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-21 13:22:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: near ( a pos -- ? ) { [ [ row ] same? ] [ [ col ] same? ] [ [ sq ] same? ] } 2|| ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 16:28:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: nth-or-lower ( n seq -- elt ) [ length 1 - 2dup > [ nip ] [ drop ] if ] keep nth ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								:: solutions ( puzzle random? -- solutions )
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 16:28:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    f puzzle random? [ indices [ f ] [ random? swap nth-or-lower ] if-empty ] [ index ] if
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ :> pos
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      1 9 [a,b] 80 iota [ pos near ] filter [ puzzle nth ] map prune diff
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 16:28:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      [ 1array puzzle pos cut-slice rest surround ] map >list [ random? solutions ] bind
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ] [ puzzle list-monad return ] if* ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: solution ( puzzle random? -- solution ) dupd solutions dup +nil+ = [ drop "Unsolvable" alert* ] [ nip car ] if ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 16:28:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hint ( puzzle -- puzzle' ) [ [ f swap indices random dup ] [ f solution ] bi nth ] keep swapd >vector [ set-nth ] keep ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: create ( difficulty -- puzzle ) 81 [ f ] replicate
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-05 16:34:31 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    40 random solution [ [ f swap [ length random ] keep set-nth ] curry times ] keep ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 16:28:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: do-sudoku ( -- ) [ [
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [
							 | 
						
					
						
							
								
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            81 [ "" ] replicate <basic> switch-models [ [ <basic> ] map 9 group [ 3 group ] map 3 group
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								               [ [ [ <spacer> [ [ <model-field> ->% 2 [ string>number ] fmap ]
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 16:28:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    map <spacer> ] map concat ] <hbox> , ] map concat <spacer> ] map concat <product>
							 | 
						
					
						
							
								
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								               [ "Difficulty:" <label> , "1" <basic> <model-field> -> [ string>number 1 or 1 + 10 * ] fmap
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								               "Generate" <model-border-btn> -> updates [ create ] fmap <spacer>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								               "Hint" <model-border-btn> -> "Solve" <model-border-btn> -> ] <hbox> ,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								               roll [ swap updates ] curry bi@
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								               [ [ hint ] fmap ] [ [ f solution ] fmap ] bi* 3array merge [ [ [ number>string ] [ "" ] if* ] map ] fmap
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								           ] bind
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 16:28:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ] with-self , ] <vbox> { 280 220 } >>pref-dim
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    "Sudoku Sleuth" open-window ] with-ui ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 17:09:23 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-30 18:11:45 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								MAIN: do-sudoku
							 |