Apologies for posting the previous message (below) earlier on Beta.
Anyway, I have now seen that there's a part two to the problem,
presumably hidden
to me as I hadn't got part one correct, and discussion of it (in
reddit.com/Advent...)
suggests that recursion IS required, or, at least, relevant.
I suppose the "2" in Pascal Jasmin's "f2" is a hint!
I hadn't intended to get involved in the Advent problems, needing to write
Christmas cards, and already busy enough with Project Euler stuff!
Mike
On 10/12/2016 09:53, 'Mike Day' via Beta wrote:
fwiw, and only about results rather than memory leaks:
,.exx NB. The worked examples provided with problem 9
+-----------------+
|ADVENT |
+-----------------+
|A(1x5)BC |
+-----------------+
|(3x3)XYZ |
+-----------------+
|A(2x2)BCD(2x2)EFG|
+-----------------+
|(6x1)(1x3)A |
+-----------------+
|X(8x2)(3x3)ABCY |
+-----------------+
unpack each exx NB. my verb produces the stated results
+-+-+-+--+-+--+
|6|7|9|11|6|18|
+-+-+-+--+-+--+
f2 each exx NB. but your results disagree with the paradigm
+-+-+-+--+-+--+
|6|7|9|11|3|20|
+-+-+-+--+-+--+
For (6x1)(1x3)A f2 goes astray at the first recursion:
(>:e)}. y
(1x3)A
... which is wrong.
You need to drop ({.i)) + >:e from y, which is empty,
and o needs to accumulate {.i
He does say "if parentheses or other chars appear within the data
referenced by a marker, that's okay - treat it like normal data,
not a marker..." I suppose that just means you don't need recursion.
BUT - my "unpack" - a naive looping approach, no recursion - apparently
overestimates the count for problem 9's file! I'm loth to look for errors
since it gets the examples right. It's over-engineered: I put in a
buffer
mechanism assuming the file would be huge and too large for array
processing,
but it's not. However, making the block size larger than the file
doesn't
alter the incorrect answer.
Typical for me - It runs fast (1 ms) and gets the wrong answer.
Mike
On 09/12/2016 14:53, 'Pascal Jasmin' via Beta wrote:
expand =: */@:[ $ {.@[ {. ]
echo f2 '(27x12)(20x12)(13x14)(7x10)(1x12)A'
echo f2 '(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN'
---
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
---
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