Reverse Game in seven lines

Damien Cassou damien.cassou at laposte.net
Wed Jul 12 09:29:03 UTC 2006


Here is the algorithm that find the shortest solution. It may be written 
in less lines I think but it works (you have to wait a bit to get the 
answer). The solution printed is the numbers you have to click on to 
solve the problem.

| state queue visited |
state _ #(4 1 9 8 6 7 5 2 3).
queue _ OrderedCollection with: (state -> OrderedCollection new).
visited _ Set new.
[queue first key isSorted]
   whileFalse: [state _ queue removeFirst.
     (visited includes: state key)
       ifFalse:
        [ visited add: state key.
          2 to: state key size do:
            [:index | |numbers|
             numbers _ state key copy.
             1 to: index//2 do: [:i | numbers swap: i with: index-i+1].
             queue addLast: (numbers -> ((OrderedCollection
            withAll: state value) add: (state key at: index); yourself)).
      ]]].

queue first value



More information about the Squeak-dev mailing list