diff options
| author | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-04-29 13:33:57 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-04-29 13:33:57 +0100 |
| commit | 844e8bf1c77f3a499b49d3be3ccab96f535ebaf3 (patch) | |
| tree | 2b1482f420d51a4fe7666be3517a57ba6f90b095 | |
| parent | fc1e5bd298e006e553960b0ba6b3797d5aa09087 (diff) | |
| download | perlweeklychallenge-club-844e8bf1c77f3a499b49d3be3ccab96f535ebaf3.tar.gz perlweeklychallenge-club-844e8bf1c77f3a499b49d3be3ccab96f535ebaf3.tar.bz2 perlweeklychallenge-club-844e8bf1c77f3a499b49d3be3ccab96f535ebaf3.zip | |
Been a busy week. Finally got this one done.
| -rw-r--r-- | challenge-058/simon-proctor/raku/ch-1.raku | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-058/simon-proctor/raku/ch-1.raku b/challenge-058/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..26c150ecf7 --- /dev/null +++ b/challenge-058/simon-proctor/raku/ch-1.raku @@ -0,0 +1,49 @@ +#!/usr/bin/env raku + +use v6; + +my $matcher = / ^ $<ver> = (\d+)+ % "." ( "_" $<patch> = (\d+) ) ? $ /; + +subset ValidVer of Str where * ~~ $matcher; + +class PWCVersion { + + has @.points; + has Int $.patch; + multi method new ( ::?CLASS:U: ValidVer $str ) { + my $match = $str ~~ $matcher; + + if defined $match[0]<patch> { + self.bless( + :points( $match<ver>.values.map(*.Int) ), + :patch( $match[0]<patch>.Int ) + ); + } else { + self.bless( + :points( $match<ver>.values.map(*.Int) ), + ); + } + } + + method Str () { + "{@!points.join('.')}{(defined $!patch)?? '_' ~ $!patch !! '' }" + } + + method gist() { self.Str } +} + +multi sub infix:<cmp> ( PWCVersion $v1, PWCVersion $v2 ) { + return $v1.points cmp $v2.points unless ($v1.points cmp $v2.points) ~~ Same; + + return Same if ! $v1.patch && ! $v2.patch; + return More if $v1.patch && ! $v2.patch; + return Less if ! $v1.patch && $v2.patch; + + return $v1.patch <=> $v2.patch; +} + +sub MAIN( ValidVer $str1, ValidVer $str2 ) { + my $v1 = PWCVersion.new($str1); + my $v2 = PWCVersion.new($str2); + say "$v1 <=> $v2 : {+($v1 cmp $v2)}"; +} |
