part 2 of adventofcode day 9, took over 1 hour to run, and in the end was left 
with 2gb+ memory use by J.

following was in a file and loaded, though an echo lines just called f2 on a 
variable holding input.  Perhaps this is faster on earlier versions of J?

issues that might cause a leak:
I am reassigning y, but I am also recursing inside a loop.

I'm not positive this code creates a leak.  I will rerun it on my machine too.  
I may have done something unrelated (or a different attempt) to consume memory.


JVERSION
Engine: j805/j64/windows
Beta-12: commercial/2016-09-30T15:50:47
Library: 8.05.09
Qt IDE: 1.5.2/5.6.2
Platform: Win 64



f2 =: 3 : 0
o =. 0
while. (# y) > e =. ')' i.~ y do.
b  =. '(' i:~ e {. y
i =. ". every 'x' cut y{~ b + >:i. <:e -b 
o =. o + b +  f2 i expand (>:e )}. y
y =. ({.i) }. (>:e) }. y
end.
o + #y
)

expand =: */@:[ $ {.@[ {. ]

echo f2 '(27x12)(20x12)(13x14)(7x10)(1x12)A'
echo f2 '(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN'
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to