#!/usr/bin/perl use strict; if (@ARGV < 1) { print STDERR "\nUsage: $0 mmddyyyy\n\n"; exit; } my ($mm, $dd, $cc, $yy) = ($ARGV[0] =~ m{(..)}g); my $ms = []; $ms->[0] = [$mm, $cc, $yy, $dd]; $ms->[1] = [$yy, $dd, $mm, $cc]; $ms->[2] = [$dd, $yy, $cc, $mm]; $ms->[3] = [$cc, $mm, $dd, $yy]; my $most = 12 + uniq($ms); if ($mm == $dd) { $most--; } if ($mm == $cc) { $most--; } if ($yy == $dd) { $most--; } if ($yy == $cc) { $most--; } while (uniq($ms) < $most) { if (int(rand(2))) { if (int(rand(2))) { rot ($ms, 1, 0, 1); rot ($ms, 1, 2, -1); } else { rot ($ms, 1, 0, -1); rot ($ms, 1, 2, 1); } } else { if (int(rand(2))) { rot ($ms, 0, 1, 1); rot ($ms, 2, 1, -1); } else { rot ($ms, 0, 1, -1); rot ($ms, 2, 1, 1); } } } show ($ms); #----------------------------------------------------------- sub uniq { my ($ms) = @_; my (%used); for my $row (0..3) { for my $col (0..3) { $used{$ms->[$row][$col]}++; } } return scalar(keys(%used)); } #----------------------------------------------------------- sub rot { my ($ms, $row, $col, $val) = @_; $ms->[$row] [$col] += $val; $ms->[$row+1][$col+1] += $val; $ms->[$row] [$col+1] -= $val; $ms->[$row+1][$col] -= $val; } #----------------------------------------------------------- sub show { my ($ms) = @_; for my $row (0..3) { for my $col (0..3) { printf(STDOUT "%2d ", $ms->[$row][$col]); } print STDOUT "\n"; } }