Neater!

Cheers,

Mike

On 31/12/2021 19:19, Raul Miller wrote:
That is a different approach.

I had avoided even thinking about that approach, because of speed
concerns. But, testing a simple implementation on the large aoc
"puzzle input", I see that this approach completes in less than 17
milliseconds, Plus it makes reasoning about the problem really simple.

Here's my implementation of your approach:

pairs=: '()','[]','{}',:'<>'
reduce=: rplc&(pairs;"1'')

    timespacex 'reduce^:_;._2 input'
0.016612 29920

And, playing with this (extending a working example until I got to the
point I wanted), part 1 looks like this:

    red=: reduce^:_;._2 sample
    'L R'=:|:pairs
    R
)]}>

    bad=: red <./@i."1 R
    cost=: 3 57 1197 25137 0 {~ R i. bad {"_1 red,.' '
    +/cost
26397

And, the second part becomes:

    require'stats'
    median 5 p.~ 5|1+L i."1 (0=cost)#red
288957

I wish I had thought of that!

Thanks,



--
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