#!/usr/bin/perl -w use strict; my @cubes= map [/./g], 'ppgygr', 'rrprgy', 'prpyyg', 'ryrgpy'; my @sides= ( '', '', '', '' ); my @base= map [/./g], '0123', '0145', '2345'; my @full; # Populate @full: for my $base ( @base ) { my @perm= @$base; for( 0..1 ) { for( 0..1 ) { for( 0..1 ) { push @full, [ @perm ]; @perm[0,1,2,3]= @perm[2,3,0,1]; # Rotate 90deg; swap X/Y } @perm[2,3]= @perm[3,2]; # Flip to reverse faces on Y axis } @perm[0,1]= @perm[1,0]; # Flip to reverse faces on X axis } die if "@perm" ne "@$base"; } # Try to explain format of the solution output: print "For cubes 1, 2, 3, and 4. The colors showing:\n"; print "frnt back left right\n"; print "1234 1234 1234 1234\n"; # Build the stack one cube at-a-time: for my $perm ( @base ) { next if ! setSides( 0, $perm ); for my $perm ( @full ) { next if ! setSides( 1, $perm ); for my $perm ( @full ) { next if ! setSides( 2, $perm ); for my $perm ( @full ) { if( setSides( 3, $perm ) ) { print "@sides\n"; } } } } } sub setSides { my( $pos, $perm )= @_; for my $cube ( $cubes[$pos] ) { for my $idx ( 0..3 ) { for my $side ( $sides[$idx] ) { for my $face ( $cube->[ $perm->[$idx] ] ) { substr( $side, $pos )= $face; return 0 if index( $side, $face ) < $pos; } } } } return 1; }