"Checkpoint 1 - copy just this expression"
| grid index row column |
grid := #(1 2 0 0 0 4 0 2 2 1 0 3 0 0 0 0).
index := (3 - 1) * 4 + 2.
row := (index - 1) // 4 + 1.
column := (index - 1) \\ 4 + 1.
{index. row. column. grid at: index.
 grid count: [:value | value = 0]}

"Checkpoint 2 - copy just this expression"
| row col box peers |
row := [:i | (i - 1) // 4 + 1].
col := [:i | (i - 1) \\ 4 + 1].
box := [:i | (((row value: i) - 1) // 2 * 2)
 + (((col value: i) - 1) // 2) + 1].
peers := (1 to: 16) collect: [:i |
 (1 to: 16) select: [:j | (i ~= j) and: [
  ((row value: i) = (row value: j)) or: [
   ((col value: i) = (col value: j)) or: [
    (box value: i) = (box value: j)]]]]].
{(peers at: 1) asArray.
 peers allSatisfy: [:group | group size = 7].
 (1 to: 16) allSatisfy: [:i | ((peers at: i) includes: i) not] }

"Checkpoint 3 - copy just this expression"
| row col box peers candidates grid |
row := [:i | (i - 1) // 4 + 1].
col := [:i | (i - 1) \\ 4 + 1].
box := [:i | (((row value: i) - 1) // 2 * 2)
 + (((col value: i) - 1) // 2) + 1].
peers := (1 to: 16) collect: [:i |
 (1 to: 16) select: [:j | (i ~= j) and: [
  ((row value: i) = (row value: j)) or: [
   ((col value: i) = (col value: j)) or: [
    (box value: i) = (box value: j)]]]]].
candidates := [:candidateGrid :i |
 ((1 to: 4) asSet difference:
  ((peers at: i) collect: [:j | candidateGrid at: j]) asSet)
  asArray sorted asArray].
grid := #(1 2 0 0 0 4 0 2 2 1 0 3 0 0 0 0).
{candidates value: grid value: 3.
 candidates value: grid value: 4.
 candidates value: grid value: 15}

"Checkpoint 4 - copy just this expression"
| row col box peers candidates grid next singles |
row := [:i | (i - 1) // 4 + 1].
col := [:i | (i - 1) \\ 4 + 1].
box := [:i | (((row value: i) - 1) // 2 * 2)
 + (((col value: i) - 1) // 2) + 1].
peers := (1 to: 16) collect: [:i |
 (1 to: 16) select: [:j | (i ~= j) and: [
  ((row value: i) = (row value: j)) or: [
   ((col value: i) = (col value: j)) or: [
    (box value: i) = (box value: j)]]]]].
candidates := [:candidateGrid :i |
 ((1 to: 4) asSet difference:
  ((peers at: i) collect: [:j | candidateGrid at: j]) asSet)
  asArray sorted asArray].
grid := #(1 2 0 0 0 4 0 2 2 1 0 3 0 0 0 0).
next := grid copy.
singles := (1 to: 16) select: [:i |
 (grid at: i) = 0 and: [(candidates value: grid value: i) size = 1]].
singles do: [:i |
 next at: i put: (candidates value: grid value: i) first].
{singles asArray. next. grid = #(1 2 0 0 0 4 0 2 2 1 0 3 0 0 0 0)}

"Checkpoint 5 - copy just this expression"
| row col box peers candidates propagate result input |
row := [:i | (i - 1) // 4 + 1].
col := [:i | (i - 1) \\ 4 + 1].
box := [:i | (((row value: i) - 1) // 2 * 2)
 + (((col value: i) - 1) // 2) + 1].
peers := (1 to: 16) collect: [:i |
 (1 to: 16) select: [:j | (i ~= j) and: [
  ((row value: i) = (row value: j)) or: [
   ((col value: i) = (col value: j)) or: [
    (box value: i) = (box value: j)]]]]].
candidates := [:candidateGrid :i |
 ((1 to: 4) asSet difference:
  ((peers at: i) collect: [:j | candidateGrid at: j]) asSet)
  asArray sorted asArray].
propagate := [:sourceGrid |
 | working snapshots outcome choices forced invalid following |
 (sourceGrid size = 16 and: [sourceGrid allSatisfy: [:v |
  v isInteger and: [v between: 0 and: 4]]])
  ifFalse: [Error signal: 'Use 16 integers in 0..4'].
 working := sourceGrid asArray copy. snapshots := OrderedCollection with: working copy.
 outcome := #running.
 [outcome = #running] whileTrue: [
  invalid := (1 to: 16) anySatisfy: [:i |
   (working at: i) ~= 0 and: [(peers at: i) anySatisfy: [:j |
    (working at: i) = (working at: j)]]].
  invalid ifTrue: [outcome := #contradiction] ifFalse: [
   choices := (1 to: 16) collect: [:i |
    (working at: i) = 0
     ifTrue: [candidates value: working value: i]
     ifFalse: [#()]].
   ((1 to: 16) anySatisfy: [:i |
    (working at: i) = 0 and: [(choices at: i) isEmpty]])
    ifTrue: [outcome := #contradiction]
    ifFalse: [
     forced := (1 to: 16) select: [:i |
      (working at: i) = 0 and: [(choices at: i) size = 1]].
     forced isEmpty
      ifTrue: [outcome := (working includes: 0)
       ifTrue: [#stuck] ifFalse: [#solved]]
      ifFalse: [
       following := working copy.
       forced do: [:i | following at: i put: (choices at: i) first].
       working := following. snapshots add: working copy]]]].
 {outcome. working. snapshots asArray}].
input := #(1 2 0 0 0 4 0 2 2 1 0 3 0 0 0 0).
result := propagate value: input.
{result first. result second. result third size - 1.
 input = #(1 2 0 0 0 4 0 2 2 1 0 3 0 0 0 0)}

"Checkpoint 6 - copy just this expression"
| row col box peers candidates propagate blank duplicate blocked |
row := [:i | (i - 1) // 4 + 1].
col := [:i | (i - 1) \\ 4 + 1].
box := [:i | (((row value: i) - 1) // 2 * 2)
 + (((col value: i) - 1) // 2) + 1].
peers := (1 to: 16) collect: [:i |
 (1 to: 16) select: [:j | (i ~= j) and: [
  ((row value: i) = (row value: j)) or: [
   ((col value: i) = (col value: j)) or: [
    (box value: i) = (box value: j)]]]]].
candidates := [:candidateGrid :i |
 ((1 to: 4) asSet difference:
  ((peers at: i) collect: [:j | candidateGrid at: j]) asSet)
  asArray sorted asArray].
propagate := [:sourceGrid |
 | working snapshots outcome choices forced invalid following |
 (sourceGrid size = 16 and: [sourceGrid allSatisfy: [:v |
  v isInteger and: [v between: 0 and: 4]]])
  ifFalse: [Error signal: 'Use 16 integers in 0..4'].
 working := sourceGrid asArray copy. snapshots := OrderedCollection with: working copy.
 outcome := #running.
 [outcome = #running] whileTrue: [
  invalid := (1 to: 16) anySatisfy: [:i |
   (working at: i) ~= 0 and: [(peers at: i) anySatisfy: [:j |
    (working at: i) = (working at: j)]]].
  invalid ifTrue: [outcome := #contradiction] ifFalse: [
   choices := (1 to: 16) collect: [:i |
    (working at: i) = 0
     ifTrue: [candidates value: working value: i]
     ifFalse: [#()]].
   ((1 to: 16) anySatisfy: [:i |
    (working at: i) = 0 and: [(choices at: i) isEmpty]])
    ifTrue: [outcome := #contradiction]
    ifFalse: [
     forced := (1 to: 16) select: [:i |
      (working at: i) = 0 and: [(choices at: i) size = 1]].
     forced isEmpty
      ifTrue: [outcome := (working includes: 0)
       ifTrue: [#stuck] ifFalse: [#solved]]
      ifFalse: [
       following := working copy.
       forced do: [:i | following at: i put: (choices at: i) first].
       working := following. snapshots add: working copy]]]].
 {outcome. working. snapshots asArray}].
blank := Array new: 16 withAll: 0.
duplicate := #(1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0).
blocked := #(0 1 2 3 4 0 0 0 0 0 0 0 0 0 0 0).
{(propagate value: blank) first.
 (propagate value: duplicate) first.
 (propagate value: blocked) first.
 candidates value: blocked value: 1}

"Checkpoint 7 - copy just this expression"
| row col box peers candidates propagate |
row := [:i | (i - 1) // 4 + 1].
col := [:i | (i - 1) \\ 4 + 1].
box := [:i | (((row value: i) - 1) // 2 * 2)
 + (((col value: i) - 1) // 2) + 1].
peers := (1 to: 16) collect: [:i |
 (1 to: 16) select: [:j | (i ~= j) and: [
  ((row value: i) = (row value: j)) or: [
   ((col value: i) = (col value: j)) or: [
    (box value: i) = (box value: j)]]]]].
candidates := [:candidateGrid :i |
 ((1 to: 4) asSet difference:
  ((peers at: i) collect: [:j | candidateGrid at: j]) asSet)
  asArray sorted asArray].
propagate := [:sourceGrid |
 | working snapshots outcome choices forced invalid following |
 (sourceGrid size = 16 and: [sourceGrid allSatisfy: [:v |
  v isInteger and: [v between: 0 and: 4]]])
  ifFalse: [Error signal: 'Use 16 integers in 0..4'].
 working := sourceGrid asArray copy. snapshots := OrderedCollection with: working copy.
 outcome := #running.
 [outcome = #running] whileTrue: [
  invalid := (1 to: 16) anySatisfy: [:i |
   (working at: i) ~= 0 and: [(peers at: i) anySatisfy: [:j |
    (working at: i) = (working at: j)]]].
  invalid ifTrue: [outcome := #contradiction] ifFalse: [
   choices := (1 to: 16) collect: [:i |
    (working at: i) = 0
     ifTrue: [candidates value: working value: i]
     ifFalse: [#()]].
   ((1 to: 16) anySatisfy: [:i |
    (working at: i) = 0 and: [(choices at: i) isEmpty]])
    ifTrue: [outcome := #contradiction]
    ifFalse: [
     forced := (1 to: 16) select: [:i |
      (working at: i) = 0 and: [(choices at: i) size = 1]].
     forced isEmpty
      ifTrue: [outcome := (working includes: 0)
       ifTrue: [#stuck] ifFalse: [#solved]]
      ifFalse: [
       following := working copy.
       forced do: [:i | following at: i put: (choices at: i) first].
       working := following. snapshots add: working copy]]]].
 {outcome. working. snapshots asArray}].
{[propagate value: #(1 2 3)] on: Error do: [:e | #rejected].
 [propagate value: (Array new: 16 withAll: 5)]
  on: Error do: [:e | #rejected].
 [propagate value: (Array new: 16 withAll: 1.5)]
  on: Error do: [:e | #rejected].
 (propagate value: (Array new: 16 withAll: 1)) first.
 (propagate value: #(1 2 3 4 3 4 1 2 2 1 4 3 4 3 2 1)) first}

"Checkpoint 8 - copy just this expression"
| row col box peers candidates propagate solution input before result ok holes conflicts |
row := [:i | (i - 1) // 4 + 1].
col := [:i | (i - 1) \\ 4 + 1].
box := [:i | (((row value: i) - 1) // 2 * 2)
 + (((col value: i) - 1) // 2) + 1].
peers := (1 to: 16) collect: [:i |
 (1 to: 16) select: [:j | (i ~= j) and: [
  ((row value: i) = (row value: j)) or: [
   ((col value: i) = (col value: j)) or: [
    (box value: i) = (box value: j)]]]]].
candidates := [:candidateGrid :i |
 ((1 to: 4) asSet difference:
  ((peers at: i) collect: [:j | candidateGrid at: j]) asSet)
  asArray sorted asArray].
propagate := [:sourceGrid |
 | working snapshots outcome choices forced invalid following |
 (sourceGrid size = 16 and: [sourceGrid allSatisfy: [:v |
  v isInteger and: [v between: 0 and: 4]]])
  ifFalse: [Error signal: 'Use 16 integers in 0..4'].
 working := sourceGrid asArray copy. snapshots := OrderedCollection with: working copy.
 outcome := #running.
 [outcome = #running] whileTrue: [
  invalid := (1 to: 16) anySatisfy: [:i |
   (working at: i) ~= 0 and: [(peers at: i) anySatisfy: [:j |
    (working at: i) = (working at: j)]]].
  invalid ifTrue: [outcome := #contradiction] ifFalse: [
   choices := (1 to: 16) collect: [:i |
    (working at: i) = 0
     ifTrue: [candidates value: working value: i]
     ifFalse: [#()]].
   ((1 to: 16) anySatisfy: [:i |
    (working at: i) = 0 and: [(choices at: i) isEmpty]])
    ifTrue: [outcome := #contradiction]
    ifFalse: [
     forced := (1 to: 16) select: [:i |
      (working at: i) = 0 and: [(choices at: i) size = 1]].
     forced isEmpty
      ifTrue: [outcome := (working includes: 0)
       ifTrue: [#stuck] ifFalse: [#solved]]
      ifFalse: [
       following := working copy.
       forced do: [:i | following at: i put: (choices at: i) first].
       working := following. snapshots add: working copy]]]].
 {outcome. working. snapshots asArray}].
solution := #(1 2 3 4 3 4 1 2 2 1 4 3 4 3 2 1).
ok := true. holes := 0. conflicts := 0.
1 to: 16 do: [:i |
 input := solution copy. input at: i put: 0. before := input copy.
 result := propagate value: input. holes := holes + 1.
 ok := ok & (result first = #solved) & (result second = solution)
  & (input = before)
  & ((propagate value: result second) second = result second)].
1 to: 16 do: [:i | (peers at: i) do: [:j | i < j ifTrue: [
 input := Array new: 16 withAll: 0.
 input at: i put: 1; at: j put: 1.
 conflicts := conflicts + 1.
 ok := ok & ((propagate value: input) first = #contradiction)]]].
ok := ok & ((propagate value: (Array new: 16 withAll: 0)) first = #stuck).
{ok. holes. conflicts}