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

Reply via email to