nice! looks clean
On Dec 11, 2015 6:56 PM, "Ryan Eckbo" <ec...@cim.mcgill.ca> wrote:

> Here's my version (&. is one of my favourite J operators):
>
> pass=:'hxbxwxba'
> alpha=: a.{~ 97+i.26
> inc=: >:&.(26&#.)&.(alpha&i.)
> nobad=: -.@:(+./)@:('iol'&e.)
> strt=: +./@:(3 (0 1 2 -: ([ - {.)@:(alpha&i.))\ ]) NB. this is ugly
> pairs=:1&<@:#@:~.@:((#~ ,&0) (2 -:/\ ]))
> good=: pairs *. strt *. nobad
> smoutput ans1=:inc^:(-.@:good)^:_ pass
> smoutput inc^:(-.@:good)^:_ inc ans1
>
>
>
> On 12 Dec 2015, at 9:22, Moon S wrote:
>
> > Such a pity J is not too popular on github.
> >
> > Anyway, here's my solution too. In general, I don't like to use tacit
> defs
> > everywhere. Procedural logic is much more clear and easier to read and
> > understand and not always slower.
> >
> > NB. next good password http://adventofcode.com/day/11
> >
> > ilo =: (+./)@(8 11 14&e.)                           NB. 'i' 'l' 'o' in
> > vector
> > abc =: 3 : '+./*./ 2=/\ 2 1 0 + 0 1 2 |.!._1"0 _ y' NB. three ascending
> > chars
> > xxs =: |:2 26$i.26                                  NB. noun: list of all
> > pairs
> > prs =: 3 : '2<:+/+./"1 xxs E."1 _ y'                NB. at least 2 diff
> > pairs
> >
> > v4s =: _97+a.&i. NB. 'hepxcrrq' --> 7 4 15 23 2 17 17 16
> > n4v =: 26&#.     NB. 7 4 15 23 2 17 17 16 --> 57647112526
> > v4n =: 26&#.^:_1 NB. 57647112526 --> 7 4 15 23 2 17 17 16
> > s4v =: a.{~ 97&+ NB. 7 4 15 23 2 17 17 16 --> 'hepxcrrq'
> >
> > main =: 3 : 0
> > n =. n4v v4s y
> > while. do. v=. v4n n=. >:n
> >   if. -.ilo v do. if. abc v do. if. prs v do. break. end. end. end. NB.
> > faster than *.?
> > end.
> > s4v v
> > )
> >
> > echo p ,: main p=. main 'hepxcrrq' NB.hepxxyzz heqaabcc
> > exit 0
> >
> >
> >
> > On Fri, Dec 11, 2015 at 5:38 PM, Joe Bogner <joebog...@gmail.com> wrote:
> >
> >> 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
> >>
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> 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