From 651f7e3f4edebae34347dad3de22a888a119b87f Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 16 Feb 2021 13:16:22 +0000 Subject: go from s{}{} -> s/// saves a byte --- challenge-100/james-smith/perl/ch-1.pl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/challenge-100/james-smith/perl/ch-1.pl b/challenge-100/james-smith/perl/ch-1.pl index 8c284ce88a..57352d1f30 100644 --- a/challenge-100/james-smith/perl/ch-1.pl +++ b/challenge-100/james-smith/perl/ch-1.pl @@ -39,22 +39,22 @@ done_testing(); ## We assume that the minute portion will be two digits after the ## ":" then we don't need to check it.... -## 111 bytes total - 103 inside the curly braces.. +## 110 bytes total - 102 inside the curly braces.. -sub ft{pop=~s{(.+)(:..)\s*(.m|)}{sprintf'%02d%s%s', -($1%12||(12*!$3))+12*('pm'eq$3),$2,$3?'':$1<12?'am':'pm'}re} +sub ft{pop=~s/(.+)(:..)\s*(.m|)/sprintf'%02d%s%s', +($1%12||(12*!$3))+12*('pm'eq$3),$2,$3?'':$1<12?'am':'pm'/re} ## This is more readable version with notes... sub fun_time { return pop =~ ## Note the nasty hack we pop rather than shift - that saves 2 bytes ## in our golfdom.... - s{ - ## Split into 3 parts, $1 - hours, $2 - minutes & $3 - am/pm + s/ + ## Split into 3 parts, $1 - hours, $2 - minutes & $3 - am-pm (.+) (:..) \s* ( .m | ) ## We assume all strings are valid - so we don't have to anchor ## at both ends or worry what the 12hr clock sufficies are - ## am/pm and .m is shorter than [ap]m + ## am-pm and .m is shorter than [ap]m ## ## We assume that the time will always have a : followed ## 2 digits... @@ -63,8 +63,7 @@ sub fun_time { ## sometimes undefined - better is to use (.m|) which ## matches the same way but $3 is now an empty string not ## undefined when we have a 24 hour clock time - } - { + / sprintf '%02d%s%s', ( $1 % 12 || ( 12 * ! $3 ) ) + 12 * ( 'pm' eq $3 ), ## Get hour modulo 12.. @@ -80,7 +79,7 @@ sub fun_time { ## The minutes is the easy bit just copy.. $3 ? '' : $1 < 12 ? 'am' : 'pm' ## If we are converting from 12hr clock the third string is - ## empty - if not and the time is <12 we return am o/w pm - }rex; + ## empty - if not and the time is <12 we return am otherwise pm + /rex; } -- cgit