Slightly different approach by converting over and back from integers and doing
some arithmetic
|.@:(+ (+/ @: (1 _1 * 40 41 =/ ]))) &. (3&u:) '1 + (2 * 3) + (4 * (5 + 6))'
((6 + 5) * 4) + (3 * 2) + 1
Cheers, bob
> On Dec 18, 2020, at 12:12, 'Michael Day' via Programming
> <[email protected]> wrote:
>
> Yes, I ended up indexing brackets with I. '()' =/ y , assuming that the
> left & right brackets
> were balanced!
>
> Part 2 was long-winded, recursive, but evaluated quickly, luckily.
>
> My slowest routine for any of these posers is for part 2 of Day 15; best I
> managed was
> ~ 40 seconds.
>
> Cheers,
>
> Mike
>
> On 18/12/2020 19:50, Joseph Novakovich wrote:
>> For part 1 I used pretty much the same approach, eval by way of
>> reversing (|.&.;:):
>>
>> +/ {{".'('c}')'o}y['o c'=. I. 0 1=/'()'i.y=.|.&.;:y}}&> in
>>
>> Mine differs mostly in avoiding library definitions, amending parens
>> by index. For part 2 I used ;: to calculate a depth vector for the
>> tokens and then I evaluate the deepest nested subexpressions at each
>> iteration reaching fixpoint.
>>
>> https://github.com/jitwit/aoc/blob/a/J/20/18.ijs
>>
>> On 12/18/20, 'Michael Day' via Programming <[email protected]> wrote:
>>> Unlike in the K forum, the J-wires have been virtually silent on this
>>> year's Advent of Code...
>>>
>>> Anyway, today's problem, number 18, might interest J & APLers as part
>>> 1 is ALMOST
>>> plug-it-in to J/APL; there's just one twist, so to speak, namely we
>>> need to reverse an
>>> arithmetic expression, eg
>>> 1 + (2 * 3) + (4 * (5 + 6))
>>> needs to be evaluated as
>>> ((6 + 5) * 4) + (3 * 2) + 1
>>>
>>> But, even though all numbers are single-digit, just reversing the
>>> string doesn't work, fairly obviously:
>>> |.'1 + (2 * 3) + (4 * (5 + 6))'
>>> ))6 + 5( * 4( + )3 * 2( + 1
>>>
>>> So the brackets need swapping. The data is supplied as strings, so I
>>> expected to use stringreplace,
>>> as found in strings.ijs
>>>
>>> But:
>>> |. ( ')[' , '()' , '[(' ) stringreplace '1 + (2 * 3) + (4 * (5
>>> + 6))'
>>> [[6 + 5) * 4) + [3 * 2) + 1
>>>
>>> Why don't the intermediate '[' get changed to the final ')' ?
>>>
>>> The second action in stringreplace is on the left hand argument:
>>> _2 [\ ,(')[' , '()' , '[(' )
>>> )[
>>> ()
>>> [(
>>>
>>> The first column should be items of old text and the second column
>>> their new replacements. But the third pair seems to be ignored.
>>>
>>> I did of course get round this little difficulty, but would have preferred
>>> not to reinvent the wheel.
>>>
>>> NB. Part 2 is a little more challenging, as + is defined there as taking
>>> precedence over * .
>>>
>>> Thanks,
>>>
>>> Mike
>>>
>>> --
>>> 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
>
>
> --
> 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