aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-10-20 23:40:52 +0100
committerGitHub <noreply@github.com>2024-10-20 23:40:52 +0100
commit83deea11dd25e53c40ba110f2064ad9e0ca8a37d (patch)
treef6bf20c9988167d2fec309dcbdbeb1a295ba1ac8
parent8167b613f6ee98ec96943d49ac810ae435a62db2 (diff)
parent07a4b3681745b6904f3573a189332d81b1a3a1a8 (diff)
downloadperlweeklychallenge-club-83deea11dd25e53c40ba110f2064ad9e0ca8a37d.tar.gz
perlweeklychallenge-club-83deea11dd25e53c40ba110f2064ad9e0ca8a37d.tar.bz2
perlweeklychallenge-club-83deea11dd25e53c40ba110f2064ad9e0ca8a37d.zip
Merge pull request #11053 from arnesom/branch-for-challenge-291
Arne Sommer
-rw-r--r--challenge-291/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-291/arne-sommer/raku/ch-1.raku26
-rwxr-xr-xchallenge-291/arne-sommer/raku/ch-2.raku92
-rwxr-xr-xchallenge-291/arne-sommer/raku/middle-index26
-rwxr-xr-xchallenge-291/arne-sommer/raku/poker-hand-rankings92
5 files changed, 237 insertions, 0 deletions
diff --git a/challenge-291/arne-sommer/blog.txt b/challenge-291/arne-sommer/blog.txt
new file mode 100644
index 0000000000..597c22d98a
--- /dev/null
+++ b/challenge-291/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/middle-hand.html
diff --git a/challenge-291/arne-sommer/raku/ch-1.raku b/challenge-291/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..cc263e432e
--- /dev/null
+++ b/challenge-291/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,26 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@ints where all(@ints) ~~ Int && @ints.elems > 0,
+ :v(:$verbose));
+
+@ints.unshift: 0;
+@ints.push: 0;
+
+for 1 .. @ints.elems - 2 -> $MI
+{
+ my @left = @ints[0 .. $MI -1];
+ my @right = @ints[$MI +1 .. *];
+
+ my $left-sum = @left.sum;
+ my $right-sum = @right.sum;
+
+ say ": MI:{ $MI -1 }: ({ @left[1..*].join(",")}) sum $left-sum <=> ({ @right[0..^*-1].join(",")}) sum $right-sum" if $verbose;
+
+ if $left-sum == $right-sum
+ {
+ say $MI -1;
+ exit;
+ }
+}
+
+say -1;
diff --git a/challenge-291/arne-sommer/raku/ch-2.raku b/challenge-291/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..44983c0231
--- /dev/null
+++ b/challenge-291/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,92 @@
+#! /usr/bin/env raku
+
+my $five_card_hands = five_card_hands;
+my $padding = $five_card_hands.chars;
+
+say "Five Card Hands: $five_card_hands\n";
+
+my %ten_ranks = ten_ranks;
+my $ten_ranks_sum = %ten_ranks.values.sum;
+
+say "Five of a Kind: { %ten_ranks<five-of-a-kind>.fmt('%' ~ $padding ~ "d") }";
+say "Straight Flush: { %ten_ranks<straight-flush>.fmt('%' ~ $padding ~ "d") }";
+say "Four of a Kind: { %ten_ranks<four-of-a-kind>.fmt('%' ~ $padding ~ "d") }";
+say "Full House: { %ten_ranks<full-house>.fmt('%' ~ $padding ~ "d") }";
+say "Flush: { %ten_ranks<flush>.fmt('%' ~ $padding ~ "d") }";
+say "Straight: { %ten_ranks<straight>.fmt('%' ~ $padding ~ "d") }";
+say "Three of a Kind: { %ten_ranks<three-of-a-kind>.fmt('%' ~ $padding ~ "d") }";
+say "Two Pair: { %ten_ranks<two-pair>.fmt('%' ~ $padding ~ "d") }";
+say "One Pair: { %ten_ranks<one-pair>.fmt('%' ~ $padding ~ "d") }";
+say "High Card: { %ten_ranks<high-card>.fmt('%' ~ $padding ~ "d") }";
+say "Sum: { $ten_ranks_sum.fmt('%' ~ $padding ~ "d") }";
+
+say $ten_ranks_sum == $five_card_hands
+ ?? "\nThe number of Five Card Hands and the Sum Matches"
+ !! "\nThe number of Five Card Hands and the Sum does not Match";
+
+sub five_card_hands
+{
+ return
+ 52 * 51 * 50 * 49 * 48 # The number of cards to choose from
+ / (5 * 4 * 3 * 2 * 1); # The order is irrelevant
+}
+
+sub ten_ranks
+{
+ my %res;
+
+ %res<five-of-a-kind> = 0; # As we do not allow jokers and wildcards
+
+ %res<straight-flush> =
+ 10 * # 10 different intervals (see below)
+ 4; # 4 suits to choose from
+
+ # Intervals: 1-5, 2-6, 3-7, 4-8, 5-9, 6-10, 7-11, 8-12, 9-13, 10-1
+
+ %res<four-of-a-kind> =
+ 13 * # 13 kinds to choose from, pick one (all four cards)
+ 12 * 4; # 12 kinds left, choose one of a any suit.
+
+ %res<full-house> =
+ 13 * 4 * # Choose three cards of the same kind (=exclude one of 4 cards)
+ 12 * 4 * 3 # Choose two cards of the same kind, different than above
+ / 2; # The order of 4,5 is irrelevant
+
+ %res<flush> =
+ 13 * 12 * 11 * 10 * 9 * # 5 cards of the same suit
+ 4 # 4 suits to choose from
+ / (5 * 4 * 3 * 2 * 1) # Order is irrelevant
+ - %res<straight-flush>; # Remove Straight Flushes
+
+ %res<straight> =
+ 4 * 4 * 4 * 4 * 4 * # Four choices for each value
+ 10 # 10 ranges (1-5 .. 9-13, 10-1)
+ - %res<straight-flush>; # Get rid of Straight Flushes
+
+ %res<three-of-a-kind> =
+ 13 * 4 * # One of thirteen, 4 possibilites for * 3
+ 12 * 4 * # Card 4; not the same value
+ 11 * 4 # Card 5; not the same valus
+ / 2; # The order of 4,5 is irrelevant
+
+ %res<two-pair> =
+ 13 * 4 * 3 # Card 1 and 2
+ / 2 * # The order of 1,2 is irrelevant
+ 12 * 4 * 3 # Card 3 and 4
+ / 2 # The order of 3,4 is irrelevant
+ / 2 * # The order of the two pairs is irrelevant
+ 11 * 4; # Card 5
+
+ %res<one-pair> =
+ 13 * 4 * 3 # Card 1 and 2.
+ / 2 * # The order of 1,2 is irrelevant
+ 12 * 4 * # Card 3, diffferent value
+ 11 * 4 * # Card 4, a second unused value
+ 10 * 4 # Card 5, a third unused value
+ / (1 * 2 * 3); # The order of 3,4,5 is irrelevant
+
+ %res<high-card> = (52 * 51 * 50 * 49 * 48) / (5 * 4 * 3 * 2 * 1)
+ - %res.values.sum;
+
+ return %res;
+}
diff --git a/challenge-291/arne-sommer/raku/middle-index b/challenge-291/arne-sommer/raku/middle-index
new file mode 100755
index 0000000000..cc263e432e
--- /dev/null
+++ b/challenge-291/arne-sommer/raku/middle-index
@@ -0,0 +1,26 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@ints where all(@ints) ~~ Int && @ints.elems > 0,
+ :v(:$verbose));
+
+@ints.unshift: 0;
+@ints.push: 0;
+
+for 1 .. @ints.elems - 2 -> $MI
+{
+ my @left = @ints[0 .. $MI -1];
+ my @right = @ints[$MI +1 .. *];
+
+ my $left-sum = @left.sum;
+ my $right-sum = @right.sum;
+
+ say ": MI:{ $MI -1 }: ({ @left[1..*].join(",")}) sum $left-sum <=> ({ @right[0..^*-1].join(",")}) sum $right-sum" if $verbose;
+
+ if $left-sum == $right-sum
+ {
+ say $MI -1;
+ exit;
+ }
+}
+
+say -1;
diff --git a/challenge-291/arne-sommer/raku/poker-hand-rankings b/challenge-291/arne-sommer/raku/poker-hand-rankings
new file mode 100755
index 0000000000..44983c0231
--- /dev/null
+++ b/challenge-291/arne-sommer/raku/poker-hand-rankings
@@ -0,0 +1,92 @@
+#! /usr/bin/env raku
+
+my $five_card_hands = five_card_hands;
+my $padding = $five_card_hands.chars;
+
+say "Five Card Hands: $five_card_hands\n";
+
+my %ten_ranks = ten_ranks;
+my $ten_ranks_sum = %ten_ranks.values.sum;
+
+say "Five of a Kind: { %ten_ranks<five-of-a-kind>.fmt('%' ~ $padding ~ "d") }";
+say "Straight Flush: { %ten_ranks<straight-flush>.fmt('%' ~ $padding ~ "d") }";
+say "Four of a Kind: { %ten_ranks<four-of-a-kind>.fmt('%' ~ $padding ~ "d") }";
+say "Full House: { %ten_ranks<full-house>.fmt('%' ~ $padding ~ "d") }";
+say "Flush: { %ten_ranks<flush>.fmt('%' ~ $padding ~ "d") }";
+say "Straight: { %ten_ranks<straight>.fmt('%' ~ $padding ~ "d") }";
+say "Three of a Kind: { %ten_ranks<three-of-a-kind>.fmt('%' ~ $padding ~ "d") }";
+say "Two Pair: { %ten_ranks<two-pair>.fmt('%' ~ $padding ~ "d") }";
+say "One Pair: { %ten_ranks<one-pair>.fmt('%' ~ $padding ~ "d") }";
+say "High Card: { %ten_ranks<high-card>.fmt('%' ~ $padding ~ "d") }";
+say "Sum: { $ten_ranks_sum.fmt('%' ~ $padding ~ "d") }";
+
+say $ten_ranks_sum == $five_card_hands
+ ?? "\nThe number of Five Card Hands and the Sum Matches"
+ !! "\nThe number of Five Card Hands and the Sum does not Match";
+
+sub five_card_hands
+{
+ return
+ 52 * 51 * 50 * 49 * 48 # The number of cards to choose from
+ / (5 * 4 * 3 * 2 * 1); # The order is irrelevant
+}
+
+sub ten_ranks
+{
+ my %res;
+
+ %res<five-of-a-kind> = 0; # As we do not allow jokers and wildcards
+
+ %res<straight-flush> =
+ 10 * # 10 different intervals (see below)
+ 4; # 4 suits to choose from
+
+ # Intervals: 1-5, 2-6, 3-7, 4-8, 5-9, 6-10, 7-11, 8-12, 9-13, 10-1
+
+ %res<four-of-a-kind> =
+ 13 * # 13 kinds to choose from, pick one (all four cards)
+ 12 * 4; # 12 kinds left, choose one of a any suit.
+
+ %res<full-house> =
+ 13 * 4 * # Choose three cards of the same kind (=exclude one of 4 cards)
+ 12 * 4 * 3 # Choose two cards of the same kind, different than above
+ / 2; # The order of 4,5 is irrelevant
+
+ %res<flush> =
+ 13 * 12 * 11 * 10 * 9 * # 5 cards of the same suit
+ 4 # 4 suits to choose from
+ / (5 * 4 * 3 * 2 * 1) # Order is irrelevant
+ - %res<straight-flush>; # Remove Straight Flushes
+
+ %res<straight> =
+ 4 * 4 * 4 * 4 * 4 * # Four choices for each value
+ 10 # 10 ranges (1-5 .. 9-13, 10-1)
+ - %res<straight-flush>; # Get rid of Straight Flushes
+
+ %res<three-of-a-kind> =
+ 13 * 4 * # One of thirteen, 4 possibilites for * 3
+ 12 * 4 * # Card 4; not the same value
+ 11 * 4 # Card 5; not the same valus
+ / 2; # The order of 4,5 is irrelevant
+
+ %res<two-pair> =
+ 13 * 4 * 3 # Card 1 and 2
+ / 2 * # The order of 1,2 is irrelevant
+ 12 * 4 * 3 # Card 3 and 4
+ / 2 # The order of 3,4 is irrelevant
+ / 2 * # The order of the two pairs is irrelevant
+ 11 * 4; # Card 5
+
+ %res<one-pair> =
+ 13 * 4 * 3 # Card 1 and 2.
+ / 2 * # The order of 1,2 is irrelevant
+ 12 * 4 * # Card 3, diffferent value
+ 11 * 4 * # Card 4, a second unused value
+ 10 * 4 # Card 5, a third unused value
+ / (1 * 2 * 3); # The order of 3,4,5 is irrelevant
+
+ %res<high-card> = (52 * 51 * 50 * 49 * 48) / (5 * 4 * 3 * 2 * 1)
+ - %res.values.sum;
+
+ return %res;
+}