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