note
frozenwithjoy
Here's my first pass... so many loops :/ (one major problem with this approach, however, is that it becomes quite memory intensive after you have about 12 elements... so, while this works, you probably want to try to build on [kennethk]'s approach.)
<c>
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Math::Combinatorics;
use List::MoreUtils qw(indexes);
use Data::Printer;
my $line = "L1 H1 L2 H2";
my @input = split /\s/, $line;
# segregate classes
my @h_vals;
my @l_vals;
for (@input) {
push @h_vals, $_ =~ /^H\d+/g;
push @l_vals, $_ =~ /^L\d+/g;
}
# index input based on classes
my @h_indexes = indexes { $_ =~ '^H\d+' } @input;
my @l_indexes = indexes { $_ =~ '^L\d+' } @input;
# permute values within classes
my @h_perms = permute @h_vals;
my @l_perms = permute @l_vals;
# get all permutations of recombined classes
my @perms;
for my $h_cur (@h_perms) {
for my $l_cur (@l_perms) {
my @current;
my @h = @{$h_cur};
my @l = @{$l_cur};
for (@h_indexes) {
$current[$_] = shift @h;
}
for (@l_indexes) {
$current[$_] = shift @l;
}
push @perms, \@current;
}
}
p @perms;
__END__
[
[0] [
[0] "L1",
[1] "H2",
[2] "L2",
[3] "H1"
],
[1] [
[0] "L2",
[1] "H2",
[2] "L1",
[3] "H1"
],
[2] [
[0] "L1",
[1] "H1",
[2] "L2",
[3] "H2"
],
[3] [
[0] "L2",
[1] "H1",
[2] "L1",
[3] "H2"
]
]
</c>
1019812
1019812