aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-112/james-smith/perl/ch-1.pl10
-rw-r--r--challenge-112/james-smith/perl/ch-2.pl29
2 files changed, 23 insertions, 16 deletions
diff --git a/challenge-112/james-smith/perl/ch-1.pl b/challenge-112/james-smith/perl/ch-1.pl
index 826c5edf30..88ff4642f7 100644
--- a/challenge-112/james-smith/perl/ch-1.pl
+++ b/challenge-112/james-smith/perl/ch-1.pl
@@ -283,7 +283,7 @@ sub canonical_path_short {
## Shorter by using regex rather than, interpolation
## and `$` rather than `/Z`
$a='';
-/^\.?$/?0:'..'ne$_?$a.="/$_":$a=~s{/[^/]+$}{} for split/\//,shift;
+/^\.?$/?0:'..'ne$_?$a.="/$_":$a=~s#/[^/]+$## for split'/',shift;
$a
}
@@ -294,7 +294,7 @@ sub canonical_path_shortest {
## or //. This is the shortest script in terms of
## bytes - but also the slowest string version.
$a='';
-'..'ne$_?$a.="/$_"x!/^\.?$/:$a=~s{/[^/]+$}{} for split/\//,shift;
+'..'ne$_?$a.="/$_"x!/^\.?$/:$a=~s#/[^/]+$## for split'/',shift;
$a
}
@@ -302,7 +302,7 @@ $a
sub canonical_path_fast {
## Skip the regex for ''/'.' and replace with compares
$a='';
-'.'ne$_&&''ne$_&&('..'ne$_?$a.="/$_":$a=~s{/[^/]+$}{})for split/\//,shift;
+'.'ne$_&&''ne$_&&('..'ne$_?$a.="/$_":$a=~s#/[^/]+$##)for split'/',shift;
$a
}
@@ -326,7 +326,7 @@ sub canonical_path_fastest {
## 16 EB (Exabytes) - I think that should be enough!
$a='';
-'.'ne$_&&''ne$_&&('..'ne$_?$a.='/'.$_:substr$a,rindex($a,'/'),~0,'')for split/\//,shift;
+'.'ne$_&&''ne$_&&('..'ne$_?$a.='/'.$_:substr$a,rindex($a,'/'),~0,'')for split'/',shift;
$a
}
@@ -338,7 +338,7 @@ sub canonical_path_global {
## this can be something useful to know in very tight
## loops...
$s='';
-'.'ne$_&&''ne$_&&('..'ne$_?$s.='/'.$_:substr$s,rindex($s,'/'),~0,'')for split/\//,shift;
+'.'ne$_&&''ne$_&&('..'ne$_?$s.='/'.$_:substr$s,rindex($s,'/'),~0,'')for split'/',shift;
$s
}
diff --git a/challenge-112/james-smith/perl/ch-2.pl b/challenge-112/james-smith/perl/ch-2.pl
index a5d78154ba..7650a2a06d 100644
--- a/challenge-112/james-smith/perl/ch-2.pl
+++ b/challenge-112/james-smith/perl/ch-2.pl
@@ -14,18 +14,22 @@ close $fh;
my $N = @ARGV ? $ARGV[0] : 30;
my $I = @ARGV > 1 ? $ARGV[1] : 100_000;
+my $cache;
-is(climb( $_), $ans[$_] ) foreach 1..$N;
-is(climb_fib( $_), $ans[$_] ) foreach 1..$N;
-is(climb_fib_1liner( $_), $ans[$_] ) foreach 1..$N;
+is(climb( $_), $ans[$_] ) foreach 1..$N;
+is(climb_fib( $_), $ans[$_] ) foreach 1..$N;
+is(climb_fib_1liner( $_), $ans[$_] ) foreach 1..$N;
+is(climb_fib_global( $_), $ans[$_] ) foreach 1..$N;
+is(climb_fib_1liner_global( $_), $ans[$_] ) foreach 1..$N;
done_testing();
cmpthese($I,{
- 'climb' => sub { climb( $_ ) foreach 0..$N; },
- 'fib-g' => sub { climb_fib_global( $_ ) foreach 0..$N; },
- 'fib-1' => sub { climb_fib_1liner( $_ ) foreach 0..$N; },
- 'fib' => sub { climb_fib( $_ ) foreach 0..$N; },
+ 'climb' => sub { climb( $_ ) foreach 0..$N; },
+ 'fib' => sub { climb_fib( $_ ) foreach 0..$N; },
+ 'fib-g' => sub { climb_fib_global( $_ ) foreach 0..$N; },
+ 'fib-1' => sub { climb_fib_1liner( $_ ) foreach 0..$N; },
+ 'fib-1g' => sub { climb_fib_1liner_global( $_ ) foreach 0..$N; },
});
## Once we look at the formula for climb - we
@@ -42,9 +46,9 @@ cmpthese($I,{
## temporarily
sub climb {
- my @climb = (1,1);
- @climb = ($climb[1],$climb[0]+$climb[1]) foreach 2..$_[0];
- return $climb[1];
+ my($a,$b) = (1,1);
+ ($a,$b) = ($b,$a+$b) foreach 2..$_[0];
+ return $b;
}
my $p;
@@ -59,6 +63,9 @@ sub climb_fib {
}
sub climb_fib_1liner {
- return int(0.001 + (($p = ((1+sqrt 5)/2)**($_[0]+1)) - ($_[0]&1?1:-1)/$p)*sqrt 0.2);
+ return int(0.001 + (($a = ((1+sqrt 5)/2)**($_[0]+1)) - ($_[0]&1?1:-1)/$a)*sqrt 0.2);
}
+sub climb_fib_1liner_global {
+ return int(0.001 + (($p = ((1+sqrt 5)/2)**($_[0]+1)) - ($_[0]&1?1:-1)/$p)*sqrt 0.2);
+}