SequentialMachine

From J Wiki
Jump to navigation Jump to search

The sequential machine can transform input into more useful tokens. A useful form for this data is a vector of the number correct and character strings for the guesses. We can obtain this by some transformations after retaining digit and space characters. ;: can output only characters from the input, rank 1 at at most.

   example=: 0 : 0   NB. http://projecteuler.net/problem=185
90342 ;2 correct
70794 ;0 correct
39458 ;2 correct
34109 ;1 correct
51545 ;2 correct
12531 ;1 correct
)

   [tokens=: (1 ; (2 2 2$1 1 0 0  1 0 0 3) ; <<'0123456789 ') ;: example
90342 2 70794 0 39458 2 34109 1 51545 2 12531 1

   't correct'=: |: _2 [\". tokens

   correct
2 0 2 1 2 1

   ]guess=: ": ,. t
90342
70794
39458
34109
51545
12531

   NB. Ha ha---now you await the solution!

   MONAD=: : [:
   DYAD=:  [: :
   tail=: {: MONAD
   ravel=: , MONAD
   box=: < MONAD
   shape_of=: $ MONAD
   copy=: # DYAD
   nub=: ~. MONAD
   ravel=: , MONAD
   tally=: # MONAD
   product=: (*/) MONAD
   unequal=: ~: DYAD
   open=: > MONAD
   set_of_atoms=: nub@:ravel
   itemize=: ,: MONAD
   all=: (*./) MONAD
   any=: (+./) MONAD
   equal=: = DYAD
   head=: {. MONAD
   link=: ; DYAD

   e185=: 4 : 0
correct=. x label_trick_APL_diamond. guess=. y
L=. length_last_dimension guess
if. correct any@:equal L do.
 NB. take first of several possible answers
 possible=. guess link/@:(head"2)@:(copy~ L&equal) correct
else.
 possible=. L (copy box@:set_of_atoms) guess
end.
while. 1 (unequal product@:(tally@open)) possible do.
 NB. something
end.
possible
)

   NB. tests ok, and solves special case puzzles.

   4 4 e185 'bcde',:'aaak'  NB. bad input with plausible result
+-+-+-+-+
|b|c|d|e|
+-+-+-+-+
    4 e185 ,:'bcde'
+-+-+-+-+
|b|c|d|e|
+-+-+-+-+
   245 e185 ,:'bbbbbbb'
+-+-+-+-+-+-+-+
|b|b|b|b|b|b|b|
+-+-+-+-+-+-+-+