#!/usr/bin/perl use strict; my $transforms = { a => { '000' => '200', '100' => '210', '200' => '220', '210' => '120', '220' => '020', '120' => '010', '020' => '000', '010' => '100', }, b => { '002' => '202', '102' => '212', '202' => '222', '212' => '122', '222' => '022', '122' => '012', '022' => '002', '012' => '102', }, c => { '000' => '020', '010' => '021', '020' => '022', '021' => '012', '022' => '002', '012' => '001', '002' => '000', '001' => '010', }, d => { '200' => '220', '210' => '221', '220' => '222', '221' => '212', '222' => '202', '212' => '201', '202' => '200', '201' => '210', }, e => { '000' => '200', '100' => '201', '200' => '202', '201' => '102', '202' => '002', '102' => '001', '002' => '000', '001' => '100', }, f => { '020' => '220', '120' => '221', '220' => '222', '221' => '122', '222' => '022', '122' => '021', '022' => '020', '021' => '120', } }; foreach my $t (keys %$transforms) { my $t_inv = {}; foreach my $from (keys %{$transforms->{$t}}) { my $to = $transforms->{$t}{$from}; $t_inv->{$to} = $from; } $t = uc($t); $transforms->{$t} = $t_inv; } my $orig = [qw( 000 001 002 010 012 020 021 022 100 102 120 122 200 201 202 210 112 220 221 222 )]; sub print_cube { my ($cube) = @_; printf " %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s ", @$cube; } sub is_equal { my ($c1, $c2) = @_; $c1 = [@$c1]; $c2 = [@$c2]; my ($e1, $e2); while ($e1 = shift @$c1) { $e2 = shift @$c2; return 0 if $e1 ne $e2; } return 1; } sub transform { my ($cube, @t) = @_; foreach my $t (@t) { $cube = [ map { $transforms->{$t}{$_} || $_ } @$cube ]; } return $cube; } sub find_cycle { my ($cube, @t) = @_; my $new_cube = [@$cube]; my $i = 0; while(1) { $i++; $new_cube = transform($new_cube, @t); #print "New Cube:"; #print_cube($new_cube); #print "\n"; return $i if is_equal($cube, $new_cube); } } sub T { my $t = shift; my @t = split //,$t; return @t; } sub rand_letter { my @choices = qw(a b c d e f A B C D E F); return $choices[rand $#choices]; } sub rand_seq { my ($num) = @_; my $str; for(1..$num) { $str .= rand_letter(); } return T($str); } my $grid; my ($x, $y); #my @choices = qw(A a B b C c D d E e F f); my @choices = qw(a A b B c C d D e E f F); foreach my $t1 (@choices) { $x++; $y = 0; print "$t1\t"; foreach my $t2 (@choices) { $y++; my @seq = ($t1, $t2); my $c = find_cycle($orig, @seq); #print "'@seq' cycles in $c moves\n"; $grid->[$x][$y] = $c; } } print "\n"; for($x = 1; $x < scalar @{$grid}; $x++) { my $row = $grid->[$x]; for($y = 1; $y < scalar @$row; $y++) { print "$grid->[$x][$y]\t"; } print "\n"; }