#!/usr/bin/perl use strict; use warnings; my @cube = map rotations($_), qw/pgpygr rprrgy ppryyg rrygpy/; for my $c1 (@{$cube[0]}) { for my $c2 (@{$cube[1]}) { for my $c3 (@{$cube[2]}) { for my $c4 (@{$cube[3]}) { my $sol = solution($c1, $c2, $c3, $c4); print "$sol\n\n" if $sol; } } } } sub rotations { my (%seen, @rot); my @cube = split //, shift @_; for ([0 .. 3], [1, 4, 3, 5], [4, 0, 5, 2]) { my @col = @cube[@$_]; push @rot, map {push @col, shift @col; $seen{"@col"}++ ? () : [@col]} 1..4; @col = reverse @{$rot[-1]}; push @rot, map {push @col, shift @col; $seen{"@col"}++ ? () : [@col]} 1..4; } return \@rot; } sub solution { my ($cube1, $cube2, $cube3, $cube4) = @_; for my $i (0 .. 3) { my %uniq = map { $_->[$i] => undef } $cube1, $cube2, $cube3, $cube4; return 0 if keys %uniq != 4; } return "@$cube1\n@$cube2\n@$cube3\n@$cube4"; }