Thanks David and Joe,

those are the right answers, but using j802's json addon on windows, makes a 
mess (appears to create extra dimensions).  Tried from file and clipboard, and 
with and without removing trailing LF.  (works in j804, but not j802 was the 
problem... hmmm actually breaks if I run it twice)


I was struggling to make a non-recursive solution.  While David's approach is 
elegant and fast, I like your approach for being able to inspect the resulting 
tree, and then seeing if anything is wrong.

This is a hard problem mostly because there are no short samples to test with.




----- Original Message -----
From: Joe Bogner <[email protected]>
To: [email protected]
Sent: Saturday, December 12, 2015 5:36 PM
Subject: Re: [Jprogramming] advent of code day 12

I think mine produces the right answer. With your input, I get the
following which matches someone else's posted solution (javascript)

  smoutput pt1=: +/ ; S:0 (traverse json)

119433

  smoutput pt2=: +/ ; S:0 (traverse json)

68466

watch the line feed on the 2nd

traverse=: (".@":@(0&{))`($: each)`(0:)@.( (2:^:(onAttr@:hasRed))^:(])(0 >~ L.))

Here's the gist of it
https://gist.github.com/joebo/7edea8f07db4596e8efb to help out with
the linefeed issue


On Sat, Dec 12, 2015 at 5:25 PM, 'Pascal Jasmin' via Programming
<[email protected]> wrote:
> I'm guessing you're ommitting a transformation of the input, but here is mine.
>
>
> https://gist.github.com/Pascal-J/bdd22d59fc07eb5a7b9a
>
>
>
>
> ----- Original Message -----
> From: Joe Bogner <[email protected]>
> To: [email protected]
> Sent: Saturday, December 12, 2015 4:58 PM
> Subject: Re: [Jprogramming] advent of code day 12
>
> I am not sure I follow. Are you saying it doesn't work in your input? If
> so, can you paste your input somewhere?
> On Dec 12, 2015 4:51 PM, "'Pascal Jasmin' via Programming" <
> [email protected]> wrote:
>
>> not working for me,
>>
>>
>> with a = dec_json input
>>
>>
>> traverse1 = your first traverse
>>
>>
>> I do get part 1 answer with either
>>
>> +/ ( 0:`]@.(2 ~:3 !:0)S:0) {. traverse1 a
>> 119433
>>
>> +/ ( 0:`]@.(2 ~:3 !:0)S:0)  a
>>
>> the reason for difference is that
>>
>>
>> $ each each traverse1 a
>>
>> includes a as its 2nd item in top row.
>>
>>
>> $ each  a
>>
>> ----- Original Message -----
>> From: Joe Bogner <[email protected]>
>> To: [email protected]
>> Sent: Saturday, December 12, 2015 3:47 PM
>> Subject: [Jprogramming] advent of code day 12
>>
>> somewhat tricky as I hadn't done recursion in J prior to this:
>>
>> require 'convert/json'
>>
>> input =: fread 'c:/joe/lang-lab/j/advent2015/day12.txt'
>>
>> json=: dec_json input
>>
>> traverse=: (".@":@(0&{))`($: each)@.(0 >~ L.)
>> smoutput pt1=: +/ ; S:0 (traverse json)
>>
>>
>> hasRed=:(<'red') e.~ [
>> NB. check for 1 in 2nd row only if it's a rank 2 array
>> NB. attributes of objects show up in the 2nd row
>> NB. we have to skip rank 1 arrays because [1,"red",5] is 6
>> onAttr=: 0:`(*@(+/)@,)@.([: 2&<: #@:$)
>> traverse=: (".@":@(0&{))`($: each)`(0:)@.( (2:^:(onAttr@:hasRed))^:(])
>> (0 >~ L.))
>> smoutput pt2=: +/ ; S:0 (traverse json)
>> ----------------------------------------------------------------------
>> 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