Matrix product is a good move. (And, there's some later puzzles where it really helps.)
(Also, I have been wishing that I had thought of using running sums here.) Thanks, -- Raul On Sun, Dec 26, 2021 at 1:17 PM Michael P. Manti <[email protected]> wrote: > > Here's my AoC Day2 solution. I first converted the course into a matrix with > one column each for moves forward, moves up, and moves down. I then used > matrix multiplications and running sums to get the horizontal movement, aim, > and depth. > > Mike > > cutspace =: [: <;._1 ' '&, > > parse =: {{)m > course =. cutspace&> cutLF y > directions =. {."1 course > distances =. ". > {:"1 course > distances *"0 1 directions ="0 1 'forward';'down';'up' > }} > > follow1 =: {{)m > horizontal =. +/\ y +/ .* 1 0 0 > depth =. +/\ y +/ .* 0 1 _1 > ({: horizontal) ; ({: depth) ; horizontal *&{: depth > }} > > follow2 =: {{)m > horizontal =. +/\ y +/ .* 1 0 0 > aim =. +/\ y +/ .* 0 1 _1 > depth =. +/\ aim * y +/ .* 1 0 0 > ({: horizontal) ; ({: aim) ; ({: depth) ; horizontal *&{: depth > }} > > > -- > Michael P. Manti > [email protected] > > > On Dec 23, 2021, at 02:41, Skip Cave <[email protected]> wrote: > > > > Here's my AoC Day 2 solution: https://adventofcode.com/2021/day/2 > > this problem seemed perfect for running sums (+/\) > > > > n=.'b' freads 'depth2.txt' NB. Read in File > > a=.'forward';'up';'down' > > s=.;".ea{:ea n NB. Get numeric values (ea = each) > > t=.+./@E.&.> NB. string match mark each. > > m=.s*"1[1 _1 1*>((0{a)t n),((1{a)t n),:((2{a)t n) NB. Set up for running > > sums > > {:+/\+/1 2{m NB. Get running depth sums > > 820 > > {:+/\0{m NB. Get running distance sums. > > 2018 > > > > 820*2018 > > 1654760 NB. Answer > > > > Skip Cave > > Cave Consulting LLC > > > > > >> On Wed, Dec 22, 2021 at 2:59 PM 'Michael Day' via Programming < > >> [email protected]> wrote: > >> > >> Apologies - more chat than programming, but: fwiw, for part 1, I just > >> defined simple > >> one-liner functions "forward" etc which worked ok. For part 2, they > >> needed redefining, and, > >> yes, I used direct defs; nothing as sophisticated as fold! > >> > >> I've done days 1-17, mostly straightforward, some very fiddly. > >> > >> But I'm stuck at 18, which is v complicated in its explanation. I > >> started with boxes in J, > >> but found navigation hard - I haven't used nested structures much in J - > >> so resorted to > >> working on the untranslated rows, using ;: to separate the symbols & > >> numbers, but > >> couldn't get all the examples to reproduce the stated results! So > >> I've wasted yet more time > >> learning - or relearning - how to deal with nested arrays in Dyalog APL > >> and remembering how > >> to use John Scholes' dfns. Managed to "reduce" the "addition" of the > >> first pair of snailnumbers, > >> but reducing the "addition" of the third line defeats me. I suppose I'm > >> sometimes inserting > >> some result at the wrong depth. > >> > >> I've spent so much time on that that I'll probably give up the rest! > >> > >> Cheers, > >> > >> Mike > >> > >> > >>> On 22/12/2021 14:59, Raul Miller wrote: > >>> (responding to > >> http://jsoftware.com/pipermail/programming/2021-December/059436.html#) > >>> > >>> I am not sure that I thought this example out well enough for it to be > >>> good on an examples page. > >>> > >>> For instance, after thinking about it for just a few minutes, I realized > >> that > >>> > >>> b2fold=: 0 0 0&(}.F..{{ > >>> 'dH0 dA0 N'=. x > >>> 'A H D'=. y > >>> dA=: N*dA0 > >>> dH=: N*dH0 > >>> dD=: A*dH > >>> y+dA,dH,dD > >>> }}) > >>> > >>> b2=: {{*/b2fold y}} > >>> > >>> would have been clearer. > >>> > >>> (Originally I was using ]F.. and commenting that I needed to drop the > >>> first item of the result. But isn't it clearer to include that drop > >>> mechanism with the fold implementation?) > >>> > >>> But (and this is worse): this particular example lacks utility, which > >>> means that it's not going to be very interesting for anyone. > >>> > >>> Other opportunities will arise, though. And I think that your concept > >>> of linked example pages is a good one and worth following up on. > >>> > >>> Thanks, > >>> > >> > >> > >> -- > >> This email has been checked for viruses by Avast antivirus software. > >> https://www.avast.com/antivirus > >> > >> ---------------------------------------------------------------------- > >> 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
