So, my solution for Masak’s p1 has the distinction of being **by far** the least efficient working solution. Which is a shame, because I think this one was my favorite solution of the contest. It may be slow, and I’d never advise anyone to use it for anything practical (given the other, much more efficient solutions), but in my opinion it’s a charming piece of code.

The key organizational piece for my solution is the `Ordering`

class. (BTW, I didn’t like that name at all, it’s probably my least favorite part of my solution. My theory is Masak was too awestruck by my inefficiency to quibble about the name.) I was striking about for how to represent a series of matrix multiplications, and hit on the idea of using a very simple stack-based language to do it. The language has two operations: an Int represents putting the matrix with that index on the stack. The string `"*"`

represents popping the top two matrices on the stack, multiplying them, and pushing the result back on the stack. Here’s the code for making that happen while tracking the total number of multiplications:

method calculate-multiplications(@matrices) { my @stack; my $total-multiplications = 0; for @.ops { when "*" { my $a = @stack.pop; my $b = @stack.pop; my ($multiplications, $matrix) = multiply($a, $b); $total-multiplications += $multiplications; @stack.push($matrix); } when Int { @stack.push(@matrices[$_]); } } $total-multiplications; }

I’m an old Forth programmer from way back, and I can’t begin to say how much I love how easy p6 makes it to implement a simple stack machine!

Getting the string version of this is equally easy:

method Str() { my @stack; for @.ops { when "*" { my $a = @stack.pop; my $b = @stack.pop; @stack.push("($a$b)"); } when Int { @stack.push("A{$_ + 1}"); } } @stack[*-1]; }

This time instead of a stack of Pairs (for the matrix size), we have a stack of Str representing each sub-matrix’s name. At the end we pop the last thing on the stack, and it’s the string representing the entire multiplication. And by making this `Ordering.Str`

, any time you print an `Ordering`

you get this nice string form — handy both for the desired output of the program and for debugging.

I won’t comment on the guts of the generate-orderings function, which is heavily borrowed from HOP via List::Utils. Just note that given the number of matrices, it lazily generates all the possible permutations — both the source of my code’s elegance and its extreme inefficiency.

Oonce you’ve got the array `@matrices`

set up, calculating and reporting the best ordering (very slowly!) is as simple as

say generate-orderings(+@matrices).min(*.calculate-multiplications(@matrices));

(Note that I split the main body of this off into a function in the actual code, to make it easier to test internally.)

So clearly, I badly need to study more dynamic programming. But at the same time, I think there may be useful bits in my code that can be put to better use somewhere else.

## Leave a Reply