diff options
| -rw-r--r-- | challenge-226/adriaandens/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-226/adriaandens/perl/ch-2.pl | 40 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-226/adriaandens/perl/ch-1.pl b/challenge-226/adriaandens/perl/ch-1.pl new file mode 100644 index 0000000000..b52ed9920e --- /dev/null +++ b/challenge-226/adriaandens/perl/ch-1.pl @@ -0,0 +1,24 @@ +use strict; +use warnings; + +use feature 'say'; + +while((my $s = <DATA>)) { + my @indices = split /, /, <DATA>; + say "Solution: " . x($s, \@indices); +} + +sub x { + my @og = split //, shift; + my $indices = shift; + my @r = (); my $i = 0; + map { $r[$_] = $og[$i++] } @{$indices}; + return join('', @r); +} + + +__DATA__ +lacelengh +3, 2, 0, 5, 4, 8, 6, 7, 1 +rulepark +4, 7, 3, 1, 0, 5, 2, 6 diff --git a/challenge-226/adriaandens/perl/ch-2.pl b/challenge-226/adriaandens/perl/ch-2.pl new file mode 100644 index 0000000000..7df3ef341a --- /dev/null +++ b/challenge-226/adriaandens/perl/ch-2.pl @@ -0,0 +1,40 @@ +use strict; +use warnings; + +use feature 'say'; + +while((my $s = <DATA>)) { + say "Solution: " . x($s); +} + + +sub x { + my @ints = split /, /, shift; + my $rounds = 0; + + my $min = get_min_bigger_than_zero(@ints); + while($min > 0) { + @ints = map { $_ > 0 ? $_ - $min : $_ } @ints; + $min = get_min_bigger_than_zero(@ints); + $rounds++; + } + + return $rounds; +} + +sub get_min_bigger_than_zero { + my @pos_ints = grep { $_ > 0 } @_; + return 0 if ! @pos_ints; # Only zeroes left + my $min = shift @pos_ints; + foreach(@pos_ints) { + if($_ < $min) { + $min = $_; + } + } + return $min; +} + +__DATA__ +1, 5, 0, 3, 5 +0 +2, 1, 4, 0, 3 |
