[Jprogramming] Advent of Code, Day 1 (spoilers)

Raul Miller rauldmiller at gmail.com
Fri Dec 1 12:23:10 UTC 2017


Actually, J gives you an easy way of making those particular expressions tacit.

For example, +/ a #~ a = (-: $ a) |. a can be made tacit by replacing
a with y and using 13 : on it. If you are particularly lazy, you can
make the replacement the =. mechanism:

   13 :'+/ a #~ a = (-: $ a) |. a=.y'
[: +/ ] #~ ] = ([: -: $) |. ]

If you want to get fancy, you can change ([: -: $) to -:@$ and there's
a few other mechanical substitutions you can make if you look for
them.

Anyways, you should not be intimidated by tacit - it's a tool and one
designed to be played with.

That said, when playing with expressions it's good to set up your
expression with a representative argument and make sure the result
stays the same with each manipulation:

   ([: +/ ] #~ ] = ([: -: $) |. ]) 1 2 3 1 2 4
6
   ([: +/ ] #~ ] = -:@$ |. ]) 1 2 3 1 2 4
6

Thanks,

-- 
Raul

On Fri, Dec 1, 2017 at 12:49 AM, Daniel Lyons <fusion at storytotell.org> wrote:
> The problem statement has to do with summing up digits in a large number if the digit matches the following digit, with wraparound.
>
> My concept is to take the input as an array of single-digit numbers, compare this to itself rotated forward a digit. Wherever those match up, the following digit is a match. I can then copy to select the values that match and then simply sum them up. It works out like this:
>
>    NB. First I want digits, so:
>    a =. "."0 '<my big captcha string>'
>
>    NB. Part 1: the idea is to line up the input against itself, rotated right one
>    +/ a #~ a = 1 |. a
>
> This worked, and then the twist in problem 2 turned out to be easy to compensate for: instead of hard-coding 1, calculate half the length of the array:
>
>    NB. Part 2: rotate by half the length
>    +/ a #~ a = (-: $ a) |. a
>
> I did not even try to define these as functions, since I was sure I'd lose the plot trying to make them tacit and get frustrated.
>
> --
> Daniel Lyons
>
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm


More information about the Programming mailing list