diff options
| author | Simon Green <mail@simon.green> | 2021-05-12 22:32:37 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2021-05-12 22:32:37 +1000 |
| commit | 0318703f17051369ab5022f7ce3c603e4dd01f1a (patch) | |
| tree | 2e41826a61f89e4cdec08d1ec68ae56f06f5a17a | |
| parent | 854ad23ac674bf18f134079573356a4fb58104a0 (diff) | |
| download | perlweeklychallenge-club-0318703f17051369ab5022f7ce3c603e4dd01f1a.tar.gz perlweeklychallenge-club-0318703f17051369ab5022f7ce3c603e4dd01f1a.tar.bz2 perlweeklychallenge-club-0318703f17051369ab5022f7ce3c603e4dd01f1a.zip | |
sgreen solution to challenge 112
| -rw-r--r-- | challenge-112/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-112/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-112/sgreen/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-112/sgreen/perl/ch-2.pl | 45 |
4 files changed, 73 insertions, 2 deletions
diff --git a/challenge-112/sgreen/README.md b/challenge-112/sgreen/README.md index 3d27abbe88..bc542f1f03 100644 --- a/challenge-112/sgreen/README.md +++ b/challenge-112/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 111 +# The Weekly Challenge 112 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-111-291m) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-112-27l0) diff --git a/challenge-112/sgreen/blog.txt b/challenge-112/sgreen/blog.txt new file mode 100644 index 0000000000..83483f8475 --- /dev/null +++ b/challenge-112/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-112-27l0 diff --git a/challenge-112/sgreen/perl/ch-1.pl b/challenge-112/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..beb77c39a6 --- /dev/null +++ b/challenge-112/sgreen/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $path = shift // die "You must specify a path\n"; + my @parts = split m^/^, $path; + my @cparts = (); + + foreach my $part (@parts) { + if ( $part eq '..' ) { + # Go up one directory (noop if top directory) + pop @cparts; + } + elsif ( $part ne '' ) { + push @cparts, $part; + } + } + + say join '/', '', @cparts; +} + +main(@ARGV); diff --git a/challenge-112/sgreen/perl/ch-2.pl b/challenge-112/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..85b7011fee --- /dev/null +++ b/challenge-112/sgreen/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub _climb { + my ( $remaining, $this_climb, $climbs ) = @_; + if ( $remaining == 1 ) { + # Add the last step to the list of climbs + push @$climbs, [ @$this_climb, 1 ]; + } + elsif ( $remaining == 2 ) { + # Add the two possible ways to climb two slips to the list + # of climbs + push @$climbs, [ @$this_climb, 1, 1 ]; + push @$climbs, [ @$this_climb, 2 ]; + } + else { + # Call the recursive function again + _climb( $remaining - 1, [ @$this_climb, 1 ], $climbs ); + _climb( $remaining - 2, [ @$this_climb, 2 ], $climbs ); + } +} + +sub main { + my $steps = shift; + my @climbs = (); + + # Call a recursive function to calculate the steps + _climb( $steps, [], \@climbs ); + + # Display the number of solutions + say "Output: ", scalar(@climbs); + say ''; + + # Display the options + my $idx = 0; + foreach my $steps (@climbs) { + ++$idx; + say "Option $idx: ", join ' + ', map { $_ == 1 ? '1 step' : '2 steps' } @$steps; + } +} + +main(@ARGV); |
