aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScimon Proctor <simon.proctor@gmail.com>2021-07-13 11:57:16 +0100
committerScimon Proctor <simon.proctor@gmail.com>2021-07-13 11:57:16 +0100
commit524a0ab00f9e4d1facd0f9ed04a3bfae4c7f673b (patch)
tree52532b257a17c57e2f3ffe6f556f873be1a86d81
parent1aa7b6eaba2a58fc1ef0612373e3aed6b61f345d (diff)
downloadperlweeklychallenge-club-524a0ab00f9e4d1facd0f9ed04a3bfae4c7f673b.tar.gz
perlweeklychallenge-club-524a0ab00f9e4d1facd0f9ed04a3bfae4c7f673b.tar.bz2
perlweeklychallenge-club-524a0ab00f9e4d1facd0f9ed04a3bfae4c7f673b.zip
Challenge 1
-rw-r--r--challenge-121/simon-proctor/raku/ch-1.raku26
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-121/simon-proctor/raku/ch-1.raku b/challenge-121/simon-proctor/raku/ch-1.raku
new file mode 100644
index 0000000000..e902c1910f
--- /dev/null
+++ b/challenge-121/simon-proctor/raku/ch-1.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+
+subset EightBit of Int where 0 <= * <= 255;
+subset ValidIdx of Int where 1 <= * <= 8;
+
+multi sub MAIN('test') is hidden-from-USAGE {
+ use Test;
+ is bit-flipped( 12, 3 ), 8;
+ is bit-flipped( 18, 4 ), 26;
+ done-testing;
+}
+
+#| Given a number between 0 and 255 and a number between 1 and 8 flips the bit at index n
+multi sub MAIN(
+ EightBit $m, #= Integer between 0 and 255
+ ValidIdx $n, #= Integer between 1 and 8
+) {
+ bit-flipped( $m, $n ).say;
+}
+
+
+sub bit-flipped( EightBit $m, ValidIdx $n ) {
+ my @l = sprintf( "%08d", $m.base(2) ).comb();
+ @l[8-$n] = 1-@l[8-$n];
+ return @l.join("").parse-base(2);
+}