my solution for day 11:

https://gist.github.com/joebo/49e9ac513c2dcac3b012


NB. creates and documents a function
NB. could be improved to split out example
NB. or automatically assert on the example
NB. originally from
http://www.jsoftware.com/pipermail/programming/2014-July/038316.html
func=: 3 : 0
doc=.(y,'_doc')=: 0 : 0
lines=.dlb each LF cut doc
0!:0 > {: lines
examples=.(y,'_examples')=:3 }. each (#~ (<'ex:') E. 3 {. each [) lines
for_ex. examples do.
    ex=.(>ex)
    try.
        assert ". ex
    catch.
        smoutput ex , ' failed'
        smoutput 'returned '
        smoutput ". ex
    end.

end.
''
)

func 'isStraight'
    boolean test for a increasing straight of 3 numbers
    restated: does adding 1 to the first 2 numbers equal the last 2 numbers?
    ex: 1 = isStraight (0,1,2)
    ex: 0 = isStraight (0,2,2)
    ex: 1 = isStraight (7,8,9)
    isStraight=: [: *@(+/) (3 ((1&+)@(2&{.) -: }.) \ ])
)

func 'nextLetter'
     returns the next letter with odometer (wrap) if necessary
     adverb parameter indicates how many letters are in the sequence
     NB.  + (97 -~ a.&i.)
     ex: 0 1 -: (2 nextLetter) (0,0)
     ex: 1 0 -: (2 nextLetter) (0,25)
     nextLetter=: (1 : '(((m#26)&#:) @: ((m#26)&#.) @: (((m#26) #: 1) + ]) )')
)

func 'charIdx'
     charIdx=: a. i. ]
)

func 'noBadLetter'
     boolean test for i, o, or l in a sequence of numbers where 0-26 = a-z
     ex: 1 = noBadLetter (97 -~ a.&i.) 'ab'
     ex: 0 = noBadLetter (97 -~ a.&i.) 'ai'
     noBadLetter=:  0&=@(+/) @: ((97 -~ (charIdx 'iol')) e. ])
)


func 'has2Pair'
     boolean test for whether there are at least 2 pairs of
non-overlapping characters
     ex: 0 = has2Pair (1,1,1,2,3)
     ex: 1 = has2Pair (1,1,1,2,2,3)
     ex: 1 = has2Pair (1,1,1,2,2,3,3)
     has2Pair=:  ((>:&2)@(+/) @: ((2 >:~ #);.1~ _1&(|.!._) ~: ]))
)


nextPassword=: (a. {~ 97&+) @: ([: (8 nextLetter)^:(-. @ (isStraight
*. noBadLetter *. has2Pair))^:_ (8 nextLetter)) @: (97 -~ a.&i.)

smoutput pt1=:nextPassword 'hepxcrrq'
smoutput pt2=:nextPassword pt1

On Fri, Dec 11, 2015 at 2:48 AM, David Lambert <b49p23t...@gmail.com> wrote:
> In the session below, the final sentence
> valid A
> runs "forever".  Why?
>
> $ ijconsole
>
>    JVERSION
> Engine: j803/2014-10-19-11:11:11
> Library: 8.04.13
> Platform: Linux 64
> Installer: unknown
> InstallPath: /usr/share/j/8.0.4
>
>
>    LC=: 26}.Alpha_j_
>
>    a2v=: _8{.LC&i.
>
>    NB. these tests apply to numerical vectors.
>    straight=: 1 e. _1 _1 E. 2 -/\ ]
>    oil=: 1 e. 14 8 11&e.
>    pairs=: ((1 < {: - {.)*.(1 < #))@:([: I. 0 = 2 -/\ ])
>    valid=: straight *. -.@:oil *. pairs
>
>    TEST=:>;:'hijklmmn abbceffg abbcegjk abcdefgh abcdffaa ghijklmn ghjaabcc'
>    ([: valid a2v)"1 TEST   NB. passes the small test suite.
> 0 0 0 0 1 0 1
>
>
>    [A=: a2v'abcdddoz'
> 0 1 2 3 3 3 14 25
>
>    valid A
> ^Z
> [1]+  Stopped                 ijconsole
> $
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to