Rafraîchissoir

By Shahed Nooshmand

Perl Weekly Challenge: week 53

After a year, I’ve finally decided to take part in the Perl Weekly Challenge. Here are my solutions for the 53rd week:

Task #1

We have the following matrix:

1 2 3
4 5 6
7 8 9

The goal is to rotate it 90/180/270 degrees clockwise.

First, here’s my somewhat one-line solution:

raku -e 'my @M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; .put for (@M = reverse(do @M[*;$_] for ^@M[0]) for ^(get() / 90 % 4 || 4))[*-1]'

I’m not saying somewhat just because it’s way longer than a normal line, but because for me “a line” means “a statement”. My solution “clearly” has two statements. I couldn’t do it with a single statement, because then I couldn’t have modified the matrix; and there’s just not enough $_.

The basis of my script is to use positive angles for counterclockwise rotations and negative angles for clockwise rotations. And I’m not limiting it to just those three angles. This is just sentimental, really.

So. We got our degrees. Since after four 90-degree rotations we’ll be back where we started, we find the number of times we have to rotate modulo 4. The || 4 is so that we don’t get Effective index out of range in case the angle is a multiple of 360.

Each time, we use each column, which is easily accessible via two-dimensional subscripting [*;$_], as a row in our matrix. After this, we reverse the matrix because otherwise it’ll look upside down. (Duh!)

Since our inner loop returns all rotations in order, we take out the last one and put it on screen, row by row.

Task #2

This is the problem:

Write a script to accept an integer 1 <= N <= 5 that would print all possible strings of size N formed by using only vowels (a, e, i, o, u).

The string should follow the following rules:

And this is the solution:

raku -e '.say for <a e i o u>.combinations(get).map(*.permutations.map(*.join))[*;*].grep({!m/ao|au|ea|eo|eu|oe|oi|ua|ui/}).sort'

It’s a pretty straightforward and kinda lame one-liner. (But at least it is one.) We find all combinations of a given size — which we get — out of the five letters we have. Then, for each combination we calculate every permutation and turn it into a word — via join. After that, we can find the ones that do not match that simple pattern, which are basically the ones that follow the rules.