diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2022-02-12 13:35:04 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2022-02-12 13:35:04 +0100 |
| commit | 64d436c521f36b32002d57b74d45b044f7773fd2 (patch) | |
| tree | c1dc7440707e84966bc4f149bf4a498cb2346588 | |
| parent | 3932c32aba8db52a503086fae4d6024d22f03434 (diff) | |
| download | perlweeklychallenge-club-64d436c521f36b32002d57b74d45b044f7773fd2.tar.gz perlweeklychallenge-club-64d436c521f36b32002d57b74d45b044f7773fd2.tar.bz2 perlweeklychallenge-club-64d436c521f36b32002d57b74d45b044f7773fd2.zip | |
Challenge 151 LK Task 1
| -rw-r--r-- | challenge-151/lubos-kolouch/perl/ch-1.pl | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-151/lubos-kolouch/perl/ch-1.pl b/challenge-151/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..ba464fb82d --- /dev/null +++ b/challenge-151/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,32 @@ +use strict; +use warnings; +use Data::Dumper; + +sub get_min_depth { + my $input = shift; + + #Input: '1 | 2 3 | 4 5' + + # iterate through the layers. If the next layer does not have 2^n items, + # there must be a leaf node + + $input =~ s/\s//g; + my @layers = split /\|/, $input; + + my $layer_count = 1; + for my $layer (@layers) { + # if not defined means we are at the last layer + return $layer_count unless defined $layers[$layer_count]; + + my $items_count = length($layers[$layer_count]); + return $layer_count unless $items_count == 2**$layer_count; + + $layer_count++; + } +} + +use Test::More; +is(get_min_depth('1 | 2 3 | 4 5'), 2); +is(get_min_depth('1 | 2 3 | 4 * * 5 | * 6'), 3); +done_testing; + |
