Funny enough, I just solved euler#67 last week:

(+ 2>./\])&.>/

It's basically the same thing as Roger proposed (I later found out) but on
a boxed representation.  Here's more detail:

   NB. Simpler data for easy visualization
   ] x =. 1 ; 1 3 ; 3 2 1 ; 1 1 3 1
+-+---+-----+-------+
|1|1 3|3 2 1|1 1 3 1|
+-+---+-----+-------+

   NB. Displays boxed data as a triangle (skip the definition
   NB. but pay attention to how the triangle representation
   NB. corresponds to the boxed data above).
   display=. > @ (((<@(#&' '))"0 @|.@i.@#) ,&.>"0 (":&.>))
   display x
   1
  1 3
 3 2 1
1 1 3 1

   NB. Solve the problem bottom up - each row is added to the
   NB. pairwise maximum of the row below it.
   solve =. (+ 2>./\])&.>/
   solve x
+-+
|9|
+-+

   NB. Traces the solution method from the bottom (right).
   NB. You can see that each row up (left) consists of the maximum
   NB. of the previous row added to the current row's values.
   NB.
   NB. Eg.
   NB. 1 1 3 1 > pairwise max > 1 3 1
   NB.               up a row > 3 2 1
   NB.                    sum > 4 5 2 > pairwise max > 5 5
   NB.                                      up a row > 1 3
   NB.                                           sum > 6 8 > etc.
   solve\. x
+-+---+-----+-------+
|9|6 8|4 5 4|1 1 3 1|
+-+---+-----+-------+

   display solve\. x
   9
  6 8
 4 5 4
1 1 3 1

Cheers,

Mike






On Mon, Jan 12, 2015 at 4:58 AM, Jon Hough <[email protected]> wrote:

>
> My solution (after studying the previous solution):
>
> NB. read file:
>
>
>
> file =: fread < '~/p067_triangle.txt'
>
> data =: cutopen file
>
> result =: ":@:(".@:[ + 2&(>./\)@:".@:])&.>/ data
> The standard library function cutopen splits the file into rows and boxes
> each row. The final verb opens the x,y operands and gets the pairwise max
> of y and adds it to x, while converting to integers. It's a bit clunky
> because I have to switch it back to literal before reboxing it.
>
>
>
>
>
> > From: [email protected]
> > Date: Sat, 10 Jan 2015 17:56:20 -0500
> > To: [email protected]
> > Subject: Re: [Jprogramming] Dear Roger Hui!
> >
> > Yeah, I miscopied the code.
> >
> > The original had >. and I used +
> >
> > Same basic structure, but I never looked back to proofread my original
> > expression.
> >
> > Also >. makes more sense than + in the context of that euler project.
> >
> > Thanks,
> >
> > --
> > Raul
> >
> > On Sat, Jan 10, 2015 at 3:30 PM, Tikkanz <[email protected]> wrote:
> > > Should that be "pairwise maximum" rather than "pairwise sum"?
> > > On 10 Jan 2015 14:55, "Raul Miller" <[email protected]> wrote:
> > >
> > >> I guess I can do a low-level view of how the code works. Though I'm
> > >> not going to use sample data from euler 67 - have to leave you
> > >> something to do... instead, I'll use
> > >>
> > >>    i.3 3
> > >> 0 1 2
> > >> 3 4 5
> > >> 6 7 8
> > >>
> > >> as the sample data.
> > >>
> > >> So that's this:
> > >>    {. (+ 0: ,~ 2: >./\ ])/ i.3 3
> > >> 12
> > >>
> > >> We also don't need : after the numbers, for this particular case, in
> > >> modern versions of J, nor do we need the spaces, and I'm going to want
> > >> this to be compact, in a moment, so:
> > >>    {. (+0,~2>./\])/ i.3 3
> > >> 12
> > >>
> > >> Let's start breaking that down. First, let's remove the 'first' verb,
> > >> so we see what it is acting on:
> > >>
> > >>    (+0,~2>./\])/ i.3 3
> > >> 12 13 2
> > >>
> > >> Now how did that happen? Well, in this case the / operator inserts its
> > >> verb between the items of its noun. So, that is:
> > >>    0 1 2 (+0,~2>./\]) 3 4 5 (+0,~2>./\]) 6 7 8
> > >> 12 13 2
> > >>
> > >> Next, let's look at just one of these cases to see how it works:
> > >>
> > >>    3 4 5 (+0,~2>./\]) 6 7 8
> > >> 10 12 5
> > >>
> > >> So how does that work? Well, if we count the verbs in (+0,~2>./\])
> > >> there's an even number of them, so the top level structure is a hook.
> > >> And, it's a dyadic hook (with an argument on its left), so that means
> > >> we can just remove the parenthesis in that sentence (need to read the
> > >> documentation on hooks, really - I'm oversimplifying the definitions
> > >> for only their relevance in this case):
> > >>
> > >>    3 4 5 +0,~2>./\] 6 7 8
> > >> 10 12 5
> > >>
> > >> In other words, it's 3 4 5 + the result of the rest of that stuff.
> > >>
> > >> So:
> > >>    0,~2>./\] 6 7 8
> > >> 7 8 0
> > >>
> > >> That's the pair-wise sum of the elements of the list, with a zero
> > >> tacked onto the right hand side.
> > >>
> > >> Again, I'm oversimplifying things here - you should be able to perform
> > >> your own experiments and/or read the definitions either in the
> > >> official dictionary
> > >> http://www.jsoftware.com/help/dictionary/vocabul.htm or in the
> > >> unofficial nuvoc http://www.jsoftware.com/jwiki/NuVoc -- but be sure
> > >> to try things out, some, to help resolve ambiguities and especially
> > >> when you think you understand things (because sometimes things are
> > >> surprising).
> > >>
> > >> So anyways, {. (+ 0: ,~ 2: >./\ ])/ m is, basically:
> > >>
> > >> The first thing from:
> > >>
> > >>    adding each row to
> > >>       the pairwise sum of
> > >>          this result for the previous row
> > >>
> > >> (padding pairwise sums on the right, to keep things the same length)
> > >>
> > >> I hope this helps.
> > >>
> > >> Thanks,
> > >>
> > >> --
> > >> Raul
> > >>
> > >> On Sat, Jan 10, 2015 at 1:06 PM, Roger Hui <[email protected]
> >
> > >> wrote:
> > >> > Hmm, I bet you a Java solution would be longer, eh? :-)
> > >> >
> > >> > It's been years since I solve Project Euler 67, and I am busy with
> other
> > >> > things on the weekend, so there'll be a delay in a detailed
> description.
> > >> > Probably others will chime in meanwhile.
> > >> >
> > >> >
> > >> >
> > >> > On Sat, Jan 10, 2015 at 9:58 AM, oventarb oventarb <
> [email protected]>
> > >> > wrote:
> > >> >
> > >> >>
> > >> >> Dear Roger Hui!
> > >> >> I solved problem 67 using Java.
> > >> >> What does this code mean:
> > >> >>
> > >> >> {. (+ 0: ,~ 2: >./\ ])/ m
> > >> >>
> ----------------------------------------------------------------------
> > >> >> 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
>
> ----------------------------------------------------------------------
> 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