Sudoku: Difference between revisions

m
→‎Immutable Backtracker: Fixing syntax highlighting
m (→‎Immutable Backtracker: Fixing syntax highlighting)
Line 4,023:
/// Outputs single line puzzle with 0 as empty squares
let asString = function
| Some values' -> values' |> Map.toSeq |> Seq.map (snd>>string) |> String.concat ""
| _ -> "No solution or Parse Failure"
 
/// Outputs puzzle in 2D format with 0 as empty squares
let prettyPrint = function
| Some (values':Map<_,_>) ->
[for r in rows do [for c in cols do (values'[key r c] |> string) ] |> String.concat " " ] |> String.concat "\n"
| _ -> "No solution or Parse Failure"
 
Line 4,043:
match s with
| None -> Some values // solved!
| Some s's2 when values[s's2] > 0 -> backtrack (next s's2) values // square not empty
| Some s's2 ->
let rec tracker (vx:Map<_,_>) dx =
match dx with
| [] -> None
| d::dx'dxrest ->
let vx'vx2 = vx |> Map.change s's2 (Option.map (fun _ -> d))
match backtrack (next s's2) vx'vx2 with
| None -> tracker vx dx'dxrest
| success -> success
[1..9]
|> List.filter (constraints values s's2)
|> tracker values