#!/usr/bin/env perl use 5.010; use strict; use warnings; use Carp; use Data::Dumper; use List::Util; $Data::Dumper::Deepcopy = 1; $Data::Dumper::Sortkeys = 1; $SIG{__DIE__} = sub { Carp::confess @_; }; $SIG{__WARN__} = sub { Carp::cluck @_; }; $| = 1; srand(); my %test_data = ( test_1 => { # Incomplete connection - 1-2-3 # # 1 - 2 - 3 # # 6 5 4 # symbol => [ 1, 2, 3, 4, 5, 6, ], segment => [ [ 1, 2, ], [ 2, 3, ], ], }, test_2 => { # Missing 1 connection - 4-5-6-1-2-3 # # 1 - 2 - 3 # \ # 6 - 5 - 4 # symbol => [ 1, 2, 3, 4, 5, 6, ], segment => [ [ 1, 2, ], [ 3, 2, ], [ 4, 5, ], [ 6, 5, ], [ 6, 1, ], ], # path_matrix => [ 1 2 3 4 5 6 # 1 - 1 0 0 0 1 # 2 - - 1 0 0 0 # 3 - - - 0 0 0 # 4 - - - - 1 0 # 5 - - - - - 1 # 6 - - - - - - }, test_3 => { # Missing 1 connection, extra connections - 1-2-3-4-5-6, 4-6 # # 1 - 2 - 3 # \ # 6 - 5 - 4 # \ / # + # symbol => [ 1, 2, 3, 4, 5, 6, ], segment => [ [ 1, 2, ], [ 2, 3, ], [ 4, 5, ], [ 5, 6, ], [ 1, 6, ], [ 4, 6, ], ], # path_matrix => [ 1 2 3 4 5 6 # 1 - 1 0 0 0 1 # 2 - - 1 0 0 0 # 3 - - - 0 0 0 # 4 - - - - 1 1 # 5 - - - - - 1 # 6 - - - - - - }, test_4 => { # Complete connection - 1-2-3-4-5-6-1 # # 1 - 2 - 3 # \ \ # 6 - 5 - 4 # symbol => [ 1, 2, 3, 4, 5, 6, ], segment => [ [ 1, 2, ], [ 2, 3, ], [ 3, 4, ], [ 4, 5, ], [ 5, 6, ], [ 6, 1, ], ], # path_matrix => [ 1 2 3 4 5 6 # 1 - 1 0 0 0 1 # 2 - - 1 0 0 0 # 3 - - - 1 0 0 # 4 - - - - 1 0 # 5 - - - - - 1 # 6 - - - - - - }, test_5 => { # Complete connection, extra connections - 1-2-3-4-5-6-1, 3-5, 3-6, 4-6 # # 1 - 2 - 3 # \ / | \ # 6 - 5 - 4 # \ / # + # symbol => [ 1, 2, 3, 4, 5, 6, ], segment => [ [ 1, 2, ], [ 2, 3, ], [ 3, 4, ], [ 4, 5, ], [ 5, 6, ], [ 6, 1, ], [ 5, 3, ], [ 6, 3, ], [ 6, 4, ], ], # path_matrix => [ 1 2 3 4 5 6 # 1 - 1 0 0 0 1 # 2 - - 1 0 0 0 # 3 - - - 1 1 1 # 4 - - - - 1 1 # 5 - - - - - 1 # 6 - - - - - - }, ); foreach my $test ( sort { $a cmp $b } keys %test_data ) { say $test; my $symbol_string = join( q{|}, @{ $test_data{$test}{symbol} }, ); my @test_run = (); foreach my $i ( 0 .. $#{ $test_data{$test}{symbol} } ) { foreach my $j ( 0 .. $#{ $test_data{$test}{symbol} } ) { $test_run[$i][$j] = 0; } } foreach my $i ( 0 .. $#{ $test_data{$test}{segment} } ) { my $x = index( $symbol_string, $test_data{$test}{segment}[$i][0], ) / 2; my $y = index( $symbol_string, $test_data{$test}{segment}[$i][1], ) / 2; ( $x, $y, ) = sort { $a <=> $b } map { $_ + 0; } ( $x, $y, ); $test_run[$x][$y]++; } foreach my $i ( 0 .. $#test_run - 1 ) { if ( List::Util::sum0( @{ $test_run[$i] } ) == 0 ) { say "Missing row $test_data{$test}{symbol}[$i]"; } } # print_compact_segment( \@{$test_data{$test}{segment}}, ); # print_compact_array_header( $test_data{$test}{symbol}, ); # print_compact_array( \@{$test_data{$test}{symbol}}, \@test_run, ); } sub print_compact_segment { my ($arr) = @_; foreach my $i ( 0 .. $#{$arr} ) { print q{[}, join( q{,}, map { sprintf( qq{%3d}, $_, ) } sort { $a <=> $b } @{ $arr->[$i] } ), q{]}, q{ }; } say q{}; } sub print_compact_array_header { my ($arr) = @_; my $str = join( q{|}, map { sprintf( qq{%3s}, $_, ) } @{$arr} ); say q{ |}, $str; say q{---+}, q{-} x length $str; } sub print_compact_array { my ( $symbol, $arr ) = @_; foreach my $i ( 0 .. $#{$arr} - 1 ) { say join( q{|}, sprintf( qq{%3s}, $symbol->[$i], ), map { sprintf( qq{%3d}, $_, ) } @{ $arr->[$i] }[ 0 .. $#{$arr} ], ); } }