diff options
| -rwxr-xr-x | challenge-333/simon-proctor/raku/ch-1.raku | 37 | ||||
| -rwxr-xr-x | challenge-333/simon-proctor/raku/ch-2.raku | 23 |
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-333/simon-proctor/raku/ch-1.raku b/challenge-333/simon-proctor/raku/ch-1.raku new file mode 100755 index 0000000000..edbc9a7a97 --- /dev/null +++ b/challenge-333/simon-proctor/raku/ch-1.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku + +class Vector2 { + has Num $.x; + has Num $.y; + has Num $.len; + + submethod BUILD(Num() :$x, Num() :$y) { + $!x = $x; + $!y = $y; + $!len = ( ($!x * $!x) + ($!y * $!y)).sqrt; + } + + method sub(Vector2 $v) { Vector2.new( x => $!x - $v.x, y => $!y - $v.y ) } + method normal() { $!len > 0 ?? Vector2.new( x => $!x / $!len, y => $!y / $!len ) !! Vector2.new(x=>0,y=>0) } + method ACCEPTS(Vector2 $v) { $!x =~= $v.x && $!y =~= $v.y } +} + +sub MAIN(:t(:$test)) is hidden-from-USAGE { + use Test; + is Vector2.new(x => 3, y => 4).len, 5.0; + ok Vector2.new(x => 1, y => 1).normal.len =~= 1.0; + ok Vector2.new(x => 1, y => 1.0) ~~ Vector2.new(x => 1.0, y => 1); + ok is-line([2, 1], [2, 3], [2, 5]); + ok is-line([1, 4], [3, 4], [10, 4]); + ok ! is-line([0, 0], [1, 1], [2, 3]); + ok is-line([1, 1], [1, 1], [1, 1]); + ok is-line([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]); + done-testing; +} + +sub is-line( **@vecs ) { + [~~] @vecs.map( -> @p { Vector2.new(x => @p[0], y => @p[1]) } ) + .rotor(2 => -1) + .map( -> ($v1, $v2) { $v1.sub($v2).normal } ); + +} diff --git a/challenge-333/simon-proctor/raku/ch-2.raku b/challenge-333/simon-proctor/raku/ch-2.raku new file mode 100755 index 0000000000..3b7b01f997 --- /dev/null +++ b/challenge-333/simon-proctor/raku/ch-2.raku @@ -0,0 +1,23 @@ +#!/usr/bin/env raku + +multi sub MAIN(:t(:$test)) is hidden-from-USAGE { + use Test; + is double-zero(1, 0, 2, 3, 0, 4, 5, 0), (1, 0, 0, 2, 3, 0, 0, 4); + is double-zero(1, 2, 3), (1, 2, 3); + is double-zero(1, 2, 3, 0), (1, 2, 3, 0); + is double-zero(0, 0, 1, 2), (0, 0, 0, 0); + is double-zero(1, 2, 0, 3, 4), (1, 2, 0, 0, 3); + done-testing; +} + +#| Given a list of integers double any zeros and return a same length list +multi sub MAIN( + *@args where all(@args) ~~ IntStr #= List of Integers +) { + say double-zero( |@args.map(*.Int) ).join(","); +} + +sub double-zero( *@args where all(@args) ~~ Int ) { + my $l = @args.elems; + return @args.map( { $_ ~~ 0 ?? |(0,0) !! $_ } )[0..^$l].List; +} |
