diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2024-05-20 17:10:19 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2024-05-20 17:10:19 +0800 |
| commit | fbd90fc53f102bf89fb3774013094193b7c54ce2 (patch) | |
| tree | fc8d0dc8179fd90113d27990a67e3bd758955d15 /challenge-270/feng-chang | |
| parent | 1a44e464db15d3896b42048a43320150afeb9d4e (diff) | |
| download | perlweeklychallenge-club-fbd90fc53f102bf89fb3774013094193b7c54ce2.tar.gz perlweeklychallenge-club-fbd90fc53f102bf89fb3774013094193b7c54ce2.tar.bz2 perlweeklychallenge-club-fbd90fc53f102bf89fb3774013094193b7c54ce2.zip | |
challenge 270, raku solutions
Diffstat (limited to 'challenge-270/feng-chang')
| -rwxr-xr-x | challenge-270/feng-chang/raku/ch-1.raku | 13 | ||||
| -rwxr-xr-x | challenge-270/feng-chang/raku/ch-2.raku | 31 | ||||
| -rwxr-xr-x | challenge-270/feng-chang/raku/test.raku | 23 |
3 files changed, 67 insertions, 0 deletions
diff --git a/challenge-270/feng-chang/raku/ch-1.raku b/challenge-270/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..4c6cd2626f --- /dev/null +++ b/challenge-270/feng-chang/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s); + +use MONKEY-SEE-NO-EVAL; + +my @a = EVAL $s; +my (\rows, \cols) = +@a, +@a[0]; +put +(^rows X ^cols).grep(-> (\b,\c) { + @a[b;c] == 1 && + +@a[b].grep(0) == cols - 1 && + +@a[*;c].grep(0) == rows - 1 +}); diff --git a/challenge-270/feng-chang/raku/ch-2.raku b/challenge-270/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..943e9fb31a --- /dev/null +++ b/challenge-270/feng-chang/raku/ch-2.raku @@ -0,0 +1,31 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my \y = @ints.pop; +my \x = @ints.pop; + +my @a = @ints.sort; +my \mx = @a[*-1]; + +if 2*x ≤ y { + put @a.map(mx - *).sum * x; + exit 0; +} + +my $score = 0; +loop { + @a .= grep(* < mx).Array; + last unless @a; + + ++@a[0]; + if +@a > 1 { + ++@a[1]; + $score += y; + } else { + $score += x; + } + + @a .= sort; +} +put $score; diff --git a/challenge-270/feng-chang/raku/test.raku b/challenge-270/feng-chang/raku/test.raku new file mode 100755 index 0000000000..d4aaadf2a4 --- /dev/null +++ b/challenge-270/feng-chang/raku/test.raku @@ -0,0 +1,23 @@ +#!/bin/env raku + +# The Weekly Challenge 270 +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, Special Positions +pwc-test './ch-1.raku', '[1,0,0],[0,0,1],[1,0,0]', 1, 'Special Positions: [[1,0,0],[0,0,1],[1,0,0]] => 1'; +pwc-test './ch-1.raku', '[1,0,0],[0,1,0],[0,0,1]', 3, 'Special Positions: [[1,0,0],[0,1,0],[0,0,1]] => 3'; + +# Task 2, Distribute Elements +pwc-test './ch-2.raku', <4 1>, 3, 2, 9, 'Distribute Elements: @ints=(4,1), $x=3, $y=2 => 9'; +pwc-test './ch-2.raku', <2 3 3 3 5>, 2, 1, 6, 'Distribute Elements: @ints=(2,3,3,3,5), $x=2, $y=1 => 6'; +pwc-test './ch-2.raku', <1 2 1>, 1, 3, 2, 'Distribute Elements: @ints=(1,2,1), $x=1, $y=3 => 2'; |
