diff options
| author | Abigail <abigail@abigail.be> | 2021-08-11 00:33:51 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-08-11 00:33:51 +0200 |
| commit | f42d586e285b5fb1694d33320380589b7709d579 (patch) | |
| tree | 17dfedb50f198749188609633b0c53e88a15015b | |
| parent | 9fc9a8a98be3336fb1bd9fb909c68af45186744e (diff) | |
| download | perlweeklychallenge-club-f42d586e285b5fb1694d33320380589b7709d579.tar.gz perlweeklychallenge-club-f42d586e285b5fb1694d33320380589b7709d579.tar.bz2 perlweeklychallenge-club-f42d586e285b5fb1694d33320380589b7709d579.zip | |
A solution for week 125, part 2, for some definition of solution.
Once again, the problem does not specify how to binary tree input
is going to look like. All we can deduce from the single example
input is that is absolutly won't scale beyond utter trivial sizes.
So, we will just die on any input which isn't exactly the one case
which is specified.
| -rw-r--r-- | challenge-125/abigail/perl/ch-2.pl | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-125/abigail/perl/ch-2.pl b/challenge-125/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..11b81d43d2 --- /dev/null +++ b/challenge-125/abigail/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +# +# Once again, the weekly challenge cannot be bothered to specify how +# the input looks like when the exercises is about binary trees; +# there's only a single example tree, of which it's clear it will not +# scale beyond anything trivial. +# +# As such, the best we can do is recognize the one example, while being +# lenient if there is surplus white space. We'll print the answer if +# the example tree is given as input, and die on anything else, as nothing +# else is specified. +# + +undef $/; +my $input = <>; + +# +# Remove blank lines +# +$input =~ s/^\s+$//gm; + +# +# Remove trailing whitespace from each line. +# +$input =~ s/\h+$//mg; + +# +# Remove leading whitespace +# +$input =~ s/^ //gm while $input !~ /^\S/m; + +if ($input eq << '--') {say 7} else {die "Unrecognized input"} + 1 + / \ + 2 5 + / \ / \ +3 4 6 7 + / \ + 8 10 + / + 9 +-- |
