Sudoku: Difference between revisions

m
Line 4,177:
/// Dictionary of all peer squares in the relevant units wrt square in question
let peers =
[for s in squares do units.[s] |> Array.concat |> Array.distinct |> Array.except [s] |> tuple2 s]
|> Map.ofList
 
/// Eliminate d from values[s] and propagate when values = 1.
/// Return Some values, except return None if a contradiction is detected.
let rec eliminate (values:Map<string_,int[]_>) (s:string) d =
let peerElim (vx:Map<string_,int[]_>) =
match Seq.length vx[s] with // (1) If a square s is reduced to one value d, then eliminate d from the peers.
| 0 -> None // removed last value
Line 4,191:
let unitsElim vx =
units[s] // (2) If a unit u is reduced to only one place for a value d, then put it there.
|> List.ofArray |> all (fun (st:Map<string_,int[]_>) u ->
let dKeys = [for s in u do if st[s] |> Seq.contains d then s]
match Seq.length dKeys with
Line 4,220:
 
/// Convert grid to a Map of constraint popagated possible values, or return None if a contradiction is detected.
let applyCPS (parsedGrid:Map<string_,int_>) =
let values = [for s in squares do s, digits]|> Map.ofList
parsedGrid