diff options
Diffstat (limited to 'challenge-286')
75 files changed, 2798 insertions, 197 deletions
diff --git a/challenge-286/0rir/ch-1.raku b/challenge-286/0rir/ch-1.raku new file mode 100755 index 0000000000..a4194bdb33 --- /dev/null +++ b/challenge-286/0rir/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use v6.d; + +=begin comment +286-1: Self Spammer +Submitted by: David Ferrone +Write a program which outputs one word of its own script / source code at random. A word is anything between whitespace, including symbols. +… +=end comment + +say $*PROGRAM-NAME.IO.slurp.words.pick; diff --git a/challenge-286/0rir/ch-2.raku b/challenge-286/0rir/ch-2.raku new file mode 100644 index 0000000000..7ea7b70d4f --- /dev/null +++ b/challenge-286/0rir/ch-2.raku @@ -0,0 +1,118 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +Put a different way, go through an array halving its size by taking, +starting at index 0, two elements and replacing them with their min. +The next two elements are replaced by their max. Continue through the +array, alternating min and max. Repeat this on each derived array until +there is one element which is the solution. + + +286-2: Order Game +Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints, whose length is a power of 2. + +Write a script to play the order game (min and max) and return the last element. + +Example 1 +Input: @ints = (2, 1, 4, 5, 6, 3, 0, 2) +Output: 1 + +Operation 1: + + min(2, 1) = 1 + max(4, 5) = 5 + min(6, 3) = 3 + max(0, 2) = 2 + +Operation 2: + + min(1, 5) = 1 + max(3, 2) = 3 + +Operation 3: + + min(1, 3) = 1 +Example 2 +Input: @ints = (0, 5, 3, 2) +Output: 0 + +Operation 1: + + min(0, 5) = 0 + max(3, 2) = 3 + +Operation 2: + + min(0, 3) = 0 +Example 3 +Input: @ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8) +Output: 2 + +Operation 1: + + min(9, 2) = 2 + max(1, 4) = 4 + min(5, 6) = 5 + max(0, 7) = 7 + min(3, 1) = 1 + max(3, 5) = 5 + min(7, 9) = 7 + max(0, 8) = 8 + +Operation 2: + + min(2, 4) = 2 + max(5, 7) = 7 + min(1, 5) = 1 + max(7, 8) = 8 + +Operation 3: + + min(2, 7) = 2 + max(1, 8) = 8 + +Operation 4: + + min(2, 8) = 2 + +=end comment + +constant @gtr-power-of-two = lazy 4, (* × 2) … 2**10; + +my @Test = + (2, 1, 4, 5, 6, 3, 0, 2), 1, + (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8), 2, + (2, 2, 1, 2, 2, 2, 2, 1), 2, + (1,2), 1, + (2,1), 1, + (2,2), 2, + (5,), 5, + (), Int, + (0, 5, 3, 2), 0, +; + +plan @Test ÷ 2; + + +sub prefix:<≭>( Bool $a is rw ) { $a = !$a } # U-226D 'not equiv to' + +multi task( @a where *.elems ≤ 2) { @a.elems == 0 ?? Int !! @a.min } +multi task( @a ) { + my Bool $f; + my @b = @a.rotor(2).map({ ≭$f ?? $_.min !! $_.max }).Array; + task( @b.Array); +} + +for @Test -> $in, $exp { + is task($in), $exp, $exp // "Int" ~ " <- " ~ $in.raku; +} + +done-testing; + +my @int = (2, 2, 1, 2, 2, 2, 2, 1); +say "\nInput: @int = @int[]\nOutput: { task @int }"; diff --git a/challenge-286/arne-sommer/blog.txt b/challenge-286/arne-sommer/blog.txt new file mode 100644 index 0000000000..d65b583a81 --- /dev/null +++ b/challenge-286/arne-sommer/blog.txt @@ -0,0 +1,2 @@ +https://raku-musings.com/self-order.html + diff --git a/challenge-286/arne-sommer/raku/ch-1.raku b/challenge-286/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..4ebe9678f6 --- /dev/null +++ b/challenge-286/arne-sommer/raku/ch-1.raku @@ -0,0 +1,9 @@ +#! /usr/bin/env raku + +unit sub MAIN (:v(:$verbose)); + +my @words = $*PROGRAM.slurp.split(/\s+/); + +say ": words: { @words.map("'" ~ * ~ "'").join(",") }" if $verbose; + +say @words.pick;
\ No newline at end of file diff --git a/challenge-286/arne-sommer/raku/ch-2.raku b/challenge-286/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..c81060961d --- /dev/null +++ b/challenge-286/arne-sommer/raku/ch-2.raku @@ -0,0 +1,33 @@ +#! /usr/bin/env raku + +my $powtwo := (1, 2, 4, 8 ... *); + +unit sub MAIN (*@ints where all(@ints) ~~ Int && @ints.elems > 0, + :v(:$verbose)); + +my $length = @ints.elems; + +for $powtwo -> $int +{ + last if $int == $length; + die 'Wrong number of @ints. Should be power of two' if $int > $length; +} + +while @ints.elems > 1 +{ + say ":\@ints: ({ @ints.join(",") })" if $verbose; + + my @new; + my $min = True; + + for @ints -> $first, $second + { + @new.push($min ?? min($first, $second) !! max($first, $second) ); + + $min = ! $min; + } + + @ints = @new; +} + +say @ints.first; diff --git a/challenge-286/arne-sommer/raku/order-game b/challenge-286/arne-sommer/raku/order-game new file mode 100755 index 0000000000..c81060961d --- /dev/null +++ b/challenge-286/arne-sommer/raku/order-game @@ -0,0 +1,33 @@ +#! /usr/bin/env raku + +my $powtwo := (1, 2, 4, 8 ... *); + +unit sub MAIN (*@ints where all(@ints) ~~ Int && @ints.elems > 0, + :v(:$verbose)); + +my $length = @ints.elems; + +for $powtwo -> $int +{ + last if $int == $length; + die 'Wrong number of @ints. Should be power of two' if $int > $length; +} + +while @ints.elems > 1 +{ + say ":\@ints: ({ @ints.join(",") })" if $verbose; + + my @new; + my $min = True; + + for @ints -> $first, $second + { + @new.push($min ?? min($first, $second) !! max($first, $second) ); + + $min = ! $min; + } + + @ints = @new; +} + +say @ints.first; diff --git a/challenge-286/arne-sommer/raku/self-spammer b/challenge-286/arne-sommer/raku/self-spammer new file mode 100755 index 0000000000..4ebe9678f6 --- /dev/null +++ b/challenge-286/arne-sommer/raku/self-spammer @@ -0,0 +1,9 @@ +#! /usr/bin/env raku + +unit sub MAIN (:v(:$verbose)); + +my @words = $*PROGRAM.slurp.split(/\s+/); + +say ": words: { @words.map("'" ~ * ~ "'").join(",") }" if $verbose; + +say @words.pick;
\ No newline at end of file diff --git a/challenge-286/asherbhs/apl/ch-2.apl b/challenge-286/asherbhs/apl/ch-2.apl new file mode 100644 index 0000000000..592f8ea98e --- /dev/null +++ b/challenge-286/asherbhs/apl/ch-2.apl @@ -0,0 +1,63 @@ +⎕IO←0 +'displayr' 'cmpx'⎕cy'dfns' + +⍝ uses some funky reshapes +OrderGame1←{ + n←≢⍵ + n≤1: ⍬ + ⌊/⊃{ + (x y)←⊂⍤2⊢1 0 2⍉⍵⍴⍨⍺,2 2 + ,⍉↑(⌊/x)(⌈/y) + }/(2*⍳(n≥4)ׯ1+2⍟n),⊂⍵ +} + +⍝ perhaps more natural, but much slower +OrderGame2←{ + ⊃{ + p←↓(⊢⍴⍨2,⍨≢÷2⍨)⍵ ⍝ ⊂⍤⊢⌺(⍪2 2)⊢⍵ + m←2|⍳≢p + ⌈/¨@{m}⌊/¨@{~m}p + }⍣(2⍟≢⍵)⊢⍵ +} + +⍝ thought this could be faster but it's actually slower +OrderGame3←{ + t←⍵,⍨0⍴⍨¯2+≢⍵ + _←2{ + i←(⍺-2)+4×⍳⍺÷4 + j←(⍵-2)+2×⍳⍵÷2 + t[j ]←t[i ]⌊t[i+1] + t[j+1]←t[i+2]⌈t[i+3] + ⍬ + }/⌽2*1+⍳2⍟≢⍵ + ⌊/2↑t +} + +⍝ slightly faster version of 2 +OrderGame4←{ + ⊃{ + m←⍬ + ⌈/¨@{m}⌊/¨@{~m⊢←2|⍳≢⍵}↓(⊢⍴⍨2,⍨≢÷2⍨)⍵ + }⍣(2⍟≢⍵)⊢⍵ +} + +⎕←OrderGame1 2 1 4 5 6 3 0 2 +⎕←OrderGame2 2 1 4 5 6 3 0 2 +⎕←OrderGame3 2 1 4 5 6 3 0 2 +⎕←OrderGame4 2 1 4 5 6 3 0 2 + +⎕←OrderGame1 0 5 3 2 +⎕←OrderGame2 0 5 3 2 +⎕←OrderGame3 0 5 3 2 +⎕←OrderGame4 0 5 3 2 + +⎕←OrderGame1 9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8 +⎕←OrderGame2 9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8 +⎕←OrderGame3 9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8 +⎕←OrderGame4 9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8 + +⍝ x←?1e4⍴⍨⎕←2*20 +⍝ ⎕profile 'start' +⍝ ⎕←cmpx 1 0 1 0/'OrderGame1 x' 'OrderGame2 x' 'OrderGame3 x' 'OrderGame4 x' +⍝ ⎕profile 'stop' +⍝ ⎕←⎕profile 'data' diff --git a/challenge-286/asherbhs/haskell/ch-2.hs b/challenge-286/asherbhs/haskell/ch-2.hs new file mode 100644 index 0000000000..5785cb8ae8 --- /dev/null +++ b/challenge-286/asherbhs/haskell/ch-2.hs @@ -0,0 +1,20 @@ +orderGame :: [Int] -> Int +orderGame ints = head $ foldr + (\_ ints -> zipWith + ($) + (cycle $ map uncurry [min, max]) + (pairUp ints) + ) + ints + [1 .. logBase 2 $ fromIntegral $ length ints] + where + pairUp :: [a] -> [(a, a)] + pairUp [] = [] + pairUp (x : y : xs) = (x, y) : pairUp xs + + +main :: IO () +main = do + print $ orderGame [2, 1, 4, 5, 6, 3, 0, 2] + print $ orderGame [0, 5, 3, 2] + print $ orderGame [9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8] diff --git a/challenge-286/asherbhs/hy/ch-2.hy b/challenge-286/asherbhs/hy/ch-2.hy new file mode 100644 index 0000000000..52bd8e02c8 --- /dev/null +++ b/challenge-286/asherbhs/hy/ch-2.hy @@ -0,0 +1,25 @@ +(require hyrule [as-> do-n fn+]) + +(import itertools [batched]) +(import math [log]) + +(defn order-game [ints] (do + (do-n + (int (log (len ints) 2)) + (setv ints (as-> ints them + (enumerate them) + (batched them 2) + (map + (fn+ [[[i x] [_ y]]] + ((if (= 0 (% i 4)) min max) x y) + ) + them + ) + ) + )) + (next ints) +)) + +(print (order-game [2 1 4 5 6 3 0 2])) +(print (order-game [0 5 3 2])) +(print (order-game [9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8])) diff --git a/challenge-286/asherbhs/raku/ch-2.raku b/challenge-286/asherbhs/raku/ch-2.raku new file mode 100644 index 0000000000..1e42470950 --- /dev/null +++ b/challenge-286/asherbhs/raku/ch-2.raku @@ -0,0 +1,19 @@ +sub order-game(@ints) { + my @r = @ints; + for ^log @r, 2 { + @r.=pairs.=map({ + ($^a.key %% 4 + ?? &[min] + !! &[max] + )( + $^a.value, + $^b.value, + ) + }) + } + @r.head +} + +say order-game (2, 1, 4, 5, 6, 3, 0, 2); +say order-game (0, 5, 3, 2); +say order-game (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8); diff --git a/challenge-286/athanasius/perl/ch-1.pl b/challenge-286/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..1023b41e30 --- /dev/null +++ b/challenge-286/athanasius/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!perl + +# Copyright © 2024 PerlMonk Athanasius + +use v5.32; +use autodie; +use List::Util 'shuffle'; + +$| = 1; +my %dict; + +open my $fh, '<', $0; + +while (my $line = <$fh>) +{ + ++$dict{$_} for grep { length } split /\s+/, $line; +} + +close $fh; +say +(shuffle keys %dict)[0]; diff --git a/challenge-286/athanasius/perl/ch-2.pl b/challenge-286/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..03a1d8b4fb --- /dev/null +++ b/challenge-286/athanasius/perl/ch-2.pl @@ -0,0 +1,246 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 286 +========================= + +TASK #2 +------- +*Order Game* + +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints, whose length is a power of 2. + +Write a script to play the order game (min and max) and return the last element. + +Example 1 + + Input: @ints = (2, 1, 4, 5, 6, 3, 0, 2) + Output: 1 + + Operation 1: + + min(2, 1) = 1 + max(4, 5) = 5 + min(6, 3) = 3 + max(0, 2) = 2 + + Operation 2: + + min(1, 5) = 1 + max(3, 2) = 3 + + Operation 3: + + min(1, 3) = 1 + +Example 2 + + Input: @ints = (0, 5, 3, 2) + Output: 0 + + Operation 1: + + min(0, 5) = 0 + max(3, 2) = 3 + + Operation 2: + + min(0, 3) = 0 + +Example 3 + + Input: @ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8) + Output: 2 + + Operation 1: + + min(9, 2) = 2 + max(1, 4) = 4 + min(5, 6) = 5 + max(0, 7) = 7 + min(3, 1) = 1 + max(3, 5) = 5 + min(7, 9) = 7 + max(0, 8) = 8 + + Operation 2: + + min(2, 4) = 2 + max(5, 7) = 7 + min(1, 5) = 1 + max(7, 8) = 8 + + Operation 3: + + min(2, 7) = 2 + max(1, 8) = 8 + + Operation 4: + + min(2, 8) = 2 + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. A non-empty list of integers is entered on the command-line. The length of + the list must be a power of two. + +=cut +#=============================================================================== + +use v5.32; # Enables strictures and warnings +use Const::Fast; +use List::Util qw( max min ); +use Regexp::Common qw( number ); +use Test::More; + |
