diff options
author | Daniel Friesel <derf@finalrewind.org> | 2020-12-20 15:56:32 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2020-12-20 15:56:32 +0100 |
commit | e1a01ac41386ae6696e62c0fcec9830347763ed4 (patch) | |
tree | 25540f503cf494cf4ac4a02bf248a0545c4fea24 | |
parent | 454d09591294dfdaa75a5364780abe525bbe77b3 (diff) |
umlauf-to-dot: add optional limit
-rwxr-xr-x | bin/umlauf-to-dot | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/bin/umlauf-to-dot b/bin/umlauf-to-dot index c884e26..69472d8 100755 --- a/bin/umlauf-to-dot +++ b/bin/umlauf-to-dot @@ -13,7 +13,7 @@ sub show_usage { my ($exit_code) = @_; say STDERR -"Usage: umlauf-to-dot <wagenreihung.json> [line number] | dot -Tpng > umlauf.png"; +"Usage: umlauf-to-dot <wagenreihung.json> [line number] [limit] | dot -Tpng > umlauf.png"; exit $exit_code; } @@ -33,17 +33,19 @@ sub format_train { my ( $train_no, $train ) = @_; return sprintf( '"%s %d\n%s\n%s"', - $train->{type} // 'Zug', + $train->{type} // $train->{raw} // 'Zug', $train_no, $train->{route}{start} // $train->{route}{preStart} // '?', $train->{route}{end} // $train->{route}{postEnd} // '?' ); } +binmode( STDOUT, ':encoding(utf-8)' ); + if ( @ARGV < 1 ) { show_usage(1); } -my ( $file, $line ) = @ARGV; +my ( $file, $line, $limit ) = @ARGV; my $file_content = read_file($file); my $json = JSON->new->utf8->decode($file_content); @@ -52,22 +54,30 @@ my $map = $json->{train}; my @train_numbers; if ($line) { - my @queue = ($line); + my @queue = ( [ $line, 0 ] ); while (@queue) { - my $train_number = pop @queue; + my ( $train_number, $distance ) = @{ pop @queue }; push( @train_numbers, $train_number ); + + if ( $limit and $distance > $limit ) { + next; + } + my @candidates; push( @candidates, @{ $map->{$train_number}{cycle}{from} // [] } ); push( @candidates, @{ $map->{$train_number}{cycle}{to} // [] } ); + @candidates = uniq @candidates; @candidates = grep { not contained_in( $_, @train_numbers, @queue ) } @candidates; + if (@candidates) { printf( "# handle %s -> push %s\n", $train_number, join( " ", @candidates ) ); } - push( @queue, @candidates ); + + push( @queue, map { [ $_, $distance + 1 ] } @candidates ); } } else { |