diff options
| author | Jan Krňávek <Jan.Krnavek@gmail.com> | 2025-10-19 20:22:20 +0200 |
|---|---|---|
| committer | Jan Krňávek <Jan.Krnavek@gmail.com> | 2025-10-19 20:22:20 +0200 |
| commit | 31372be9fef1c1277caf802b23e8ab3389898e70 (patch) | |
| tree | 1a134238c7f1dc5b21158013649cabcd8d5bec11 | |
| parent | bd396320750457f49888b9938e057eac5d59f5f8 (diff) | |
| download | perlweeklychallenge-club-31372be9fef1c1277caf802b23e8ab3389898e70.tar.gz perlweeklychallenge-club-31372be9fef1c1277caf802b23e8ab3389898e70.tar.bz2 perlweeklychallenge-club-31372be9fef1c1277caf802b23e8ab3389898e70.zip | |
solutions week 343
| -rw-r--r-- | challenge-343/wambash/raku/ch-1.raku | 32 | ||||
| -rw-r--r-- | challenge-343/wambash/raku/ch-2.raku | 41 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-343/wambash/raku/ch-1.raku b/challenge-343/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..4bdc5aa647 --- /dev/null +++ b/challenge-343/wambash/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku + +sub zero-friend (+nums) { + nums + andthen .map: *.abs + andthen .min +} + +multi MAIN (Bool :test($)!) { + use Test; + + # Example cases from the challenge + is zero-friend((4, 2, -1, 3, -2)), 1, 'closest to zero are -1 (1 away) and 2 (2 away) → result 1'; + is zero-friend((-5, 5, -3, 3, -1, 1)), 1, 'closest are -1 and 1 → distance 1'; + is zero-friend((7, -3, 0, 2, -8)), 0, 'exact zero present → distance 0'; + is zero-friend((-2, -5, -1, -8)), 1, 'closest are -1 (1 away) and -2 (2 away) → result 1'; + is zero-friend((-2, 2, -4, 4, -1, 1)), 1, 'closest are -1 and 1 → distance 1'; + + # Additional edge cases + is zero-friend((0)), 0, 'single element zero → distance 0'; + is zero-friend((10)), 10, 'single positive number → distance 10'; + is zero-friend((-7)), 7, 'single negative number → distance 7'; + is zero-friend((100, -99)), 99, 'closest is -99 → distance 99'; + + done-testing; +} + + + +multi MAIN (+nums) { + say zero-friend nums; +} diff --git a/challenge-343/wambash/raku/ch-2.raku b/challenge-343/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..663bcd20e8 --- /dev/null +++ b/challenge-343/wambash/raku/ch-2.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku + +sub champion-team (+grid) { + my %adepts := ( + grid + andthen .max: *.cache.sum, :p + andthen .Map + ); + + %adepts + andthen .nodemap: { $_[%adepts.keys] }\ + andthen .max: *.value.sum + andthen .key +} + +multi MAIN (Bool :test($)!) { + use Test; + + # Example cases from the challenge + is champion-team([ [0,1,1], [0,0,1], [0,0,0] ]), 0, + 'Team 0 beats Team 1 and 2 → champion 0'; + is champion-team([ [0,1,0,0], [0,0,0,0], [1,1,0,0], [1,1,1,0] ]), 3, + 'Team 3 beats everyone → champion 3'; + is champion-team([ [0,1,0,1], [0,0,1,1], [1,0,0,0], [0,0,1,0] ]), 0, + 'Teams 0 and 1 tie with 2 wins, but 0 beats 1 → champion 0'; + is champion-team([ [0,1,1], [0,0,0], [0,1,0] ]), 0, + 'Team 0 beats both others → champion 0'; + is champion-team([ [0,0,0,0,0], [1,0,0,0,0], [1,1,0,1,1], [1,1,0,0,0], [1,1,0,1,0] ]), 2, + 'Team 2 beats almost everyone → champion 2'; + + # Additional edge cases + is champion-team([ [0] ]), 0, 'single team → champion 0'; + is champion-team([ [0,1], [0,0] ]), 0, 'two teams: team 0 beats team 1 → champion 0'; + is champion-team([ [0,0], [1,0] ]), 1, 'two teams: team 1 beats team 0 → champion 1'; + + done-testing; +} + +multi MAIN (+grid) { + say champion-team grid».comb: / 0|1 /; +} |
