diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-06 09:55:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-06 09:55:03 +0100 |
| commit | c3cc4bdd7ad1d35b9b32ebaab035a5130290267f (patch) | |
| tree | be82dc8d4b1296318b732a656d7a13d53c2db56c | |
| parent | a859c739d76f7c9290f4f8b9bd2a47fc2435ffd5 (diff) | |
| parent | e76534a69d6bf5ca84511a5e0d093539a83abedb (diff) | |
| download | perlweeklychallenge-club-c3cc4bdd7ad1d35b9b32ebaab035a5130290267f.tar.gz perlweeklychallenge-club-c3cc4bdd7ad1d35b9b32ebaab035a5130290267f.tar.bz2 perlweeklychallenge-club-c3cc4bdd7ad1d35b9b32ebaab035a5130290267f.zip | |
Merge pull request #10555 from seaker/master
challenge 281, raku solutions
| -rwxr-xr-x | challenge-281/feng-chang/raku/ch-1.raku | 7 | ||||
| -rwxr-xr-x | challenge-281/feng-chang/raku/ch-2.raku | 25 | ||||
| -rwxr-xr-x | challenge-281/feng-chang/raku/test.raku | 23 |
3 files changed, 55 insertions, 0 deletions
diff --git a/challenge-281/feng-chang/raku/ch-1.raku b/challenge-281/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..ba6c17b454 --- /dev/null +++ b/challenge-281/feng-chang/raku/ch-1.raku @@ -0,0 +1,7 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $coord where *.chars == 2); + +put do with $coord.comb -> ($c, $n) { + ($c.ord - 'a'.ord + $n) %% 2 +} diff --git a/challenge-281/feng-chang/raku/ch-2.raku b/challenge-281/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..43c506b0f1 --- /dev/null +++ b/challenge-281/feng-chang/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $p1 where *.chars == 2, Str:D $p2 where *.chars == 2); + +with |$p1.comb, |$p2.comb -> (\c1,\d1,\c2,\d2) { + put dist(abs(c1.ord - c2.ord), abs(d1 - d2)); +} + +proto _dist(Int:D \x, Int:D \y --> UInt:D) {*} +multi _dist(\x where * < 0, $_) { Inf } +multi _dist($_, \y where * < 0) { Inf } +multi _dist(0, 0) { 0 } +multi _dist(0, 1) { 3 } +multi _dist(1, 1) { 2 } +multi _dist(0, 2) { 2 } +multi _dist(\x, \y where x > y) { _dist(y, x) } +multi _dist(\x, \y) { min(_dist(x - 1, y - 2), _dist(x - 2, y - 1)) + 1 } + +sub dist(UInt:D \x, UInt:D \y --> UInt:D) { + state %dists; + my \K = "{x},{y}"; + %dists{K} = _dist(x, y) without %dists{K}; + + %dists{K} +} diff --git a/challenge-281/feng-chang/raku/test.raku b/challenge-281/feng-chang/raku/test.raku new file mode 100755 index 0000000000..c718d77f78 --- /dev/null +++ b/challenge-281/feng-chang/raku/test.raku @@ -0,0 +1,23 @@ +#!/bin/env raku + +# The Weekly Challenge 281 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Check Color +pwc-test './ch-1.raku', 'd3', 'True', 'Check Color: d3 => true'; +pwc-test './ch-1.raku', 'g5', 'False', 'Check Color: g5 => false'; +pwc-test './ch-1.raku', 'e6', 'True', 'Check Color: e6 => true'; + +# Task 2, Knight's Move +pwc-test './ch-2.raku', <g2 a8>, 4, "Knight\'s Move: g2, a8 => 4"; +pwc-test './ch-2.raku', <g2 h2>, 3, "Knight\'s Move: g2, h2 => 3"; |
