aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-057/e-choroba/perl/ch-1.pl24
-rwxr-xr-xchallenge-057/e-choroba/perl/ch-1a.pl17
-rwxr-xr-xchallenge-057/e-choroba/perl/ch-1b.pl19
3 files changed, 18 insertions, 42 deletions
diff --git a/challenge-057/e-choroba/perl/ch-1.pl b/challenge-057/e-choroba/perl/ch-1.pl
index 5af1aa8245..f726f44b8a 100755
--- a/challenge-057/e-choroba/perl/ch-1.pl
+++ b/challenge-057/e-choroba/perl/ch-1.pl
@@ -1,25 +1,17 @@
#!/usr/bin/perl
use warnings;
use strict;
-use feature qw{ say };
use ARGV::OrDATA;
-use FindBin;
-use lib $FindBin::Bin;
-use Tree qw{ from_structure };
+my %invert = qw( L R R L );
-sub invert {
- my ($node, $tree) = @_;
- return $node unless exists $tree->{$node};
-
- my @ch = reverse @{ $tree->{$node} };
- return $node . '(' . join(',', map invert($_, $tree), @ch) . ')'
-}
-
-chomp( my $structure = <> );
-my ($root, $tree) = from_structure($structure);
-say '(', invert($root, $tree), ')';
+print s/ ([LR]) / $invert{$1} /r while <DATA>;
__DATA__
-(1(2(4,5),3(6,7)))
+4 L 2
+5 R 2
+6 L 3
+7 R 3
+2 L 1
+3 R 1
diff --git a/challenge-057/e-choroba/perl/ch-1a.pl b/challenge-057/e-choroba/perl/ch-1a.pl
deleted file mode 100755
index f726f44b8a..0000000000
--- a/challenge-057/e-choroba/perl/ch-1a.pl
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/usr/bin/perl
-use warnings;
-use strict;
-
-use ARGV::OrDATA;
-
-my %invert = qw( L R R L );
-
-print s/ ([LR]) / $invert{$1} /r while <DATA>;
-
-__DATA__
-4 L 2
-5 R 2
-6 L 3
-7 R 3
-2 L 1
-3 R 1
diff --git a/challenge-057/e-choroba/perl/ch-1b.pl b/challenge-057/e-choroba/perl/ch-1b.pl
index 5af1aa8245..ba8bffc893 100755
--- a/challenge-057/e-choroba/perl/ch-1b.pl
+++ b/challenge-057/e-choroba/perl/ch-1b.pl
@@ -3,12 +3,6 @@ use warnings;
use strict;
use feature qw{ say };
-use ARGV::OrDATA;
-
-use FindBin;
-use lib $FindBin::Bin;
-use Tree qw{ from_structure };
-
sub invert {
my ($node, $tree) = @_;
return $node unless exists $tree->{$node};
@@ -17,9 +11,16 @@ sub invert {
return $node . '(' . join(',', map invert($_, $tree), @ch) . ')'
}
-chomp( my $structure = <> );
-my ($root, $tree) = from_structure($structure);
-say '(', invert($root, $tree), ')';
+chomp( my $structure = <DATA> );
+
+my %tree;
+while ($structure =~ s/([0-9]+) \( ([0-9]+) , ([0-9]+) \) /$1/x) {
+ my ($parent, $left, $right) = ($1, $2, $3);
+ $tree{$parent} = [$left, $right];
+}
+
+my ($root) = $structure =~ /[0-9]+/;
+say '(', invert($root, \%tree), ')';
__DATA__
(1(2(4,5),3(6,7)))