aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/colin-crain/raku
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-05-09 09:04:03 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-05-09 09:04:03 +0100
commitde0ca0b00cad07ea6ec6a30d3b12207efd1e55e7 (patch)
treea5254b6a4b0d08c6493b090a4b4ef34304cdec2e /challenge-059/colin-crain/raku
parentd67e3e2c60369a2c33e42a215374f907a24cc19d (diff)
downloadperlweeklychallenge-club-de0ca0b00cad07ea6ec6a30d3b12207efd1e55e7.tar.gz
perlweeklychallenge-club-de0ca0b00cad07ea6ec6a30d3b12207efd1e55e7.tar.bz2
perlweeklychallenge-club-de0ca0b00cad07ea6ec6a30d3b12207efd1e55e7.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-059/colin-crain/raku')
-rw-r--r--challenge-059/colin-crain/raku/ch-1.p665
-rw-r--r--challenge-059/colin-crain/raku/ch-2.p611
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-059/colin-crain/raku/ch-1.p6 b/challenge-059/colin-crain/raku/ch-1.p6
new file mode 100644
index 0000000000..b6c5975aee
--- /dev/null
+++ b/challenge-059/colin-crain/raku/ch-1.p6
@@ -0,0 +1,65 @@
+class Node {
+ has Int $.value is rw;
+ has Node $.next is rw;
+}
+
+class LinkedList {
+ has Node $.first is rw;
+ has Node $.last is rw;
+
+ method populate_from_array ( @array ) {
+ my $node;
+ my $next;
+ while @array.elems > 0 {
+ $node = Node.new(value => @array.pop.Int);
+ $node.next = $next if $next.defined;
+ $next = $node;
+ }
+ $.first = $node;
+ }
+
+ method arrow_print () {
+ my @output;
+ my $node = $.first;
+ while (defined $node) {
+ push @output, $node.value;
+ $node = $node.next;
+ }
+ @output.join(' → ').say;
+ }
+}
+
+
+sub MAIN (Int:D $locus, *@input) {
+
+ ## 1. convert the input commandline array into a linked list
+ my $list = LinkedList.new();
+ $list.populate_from_array( @input );
+
+ my $before = LinkedList.new();
+ my $after = LinkedList.new();
+ my $node = $list.first;
+
+ ## 2a. if it is less than the given value, add it to
+ ## the end of the before list
+ ## 2b. if it is more than or equal to the given value
+ ## add it to the end of the after list
+ ## if a sublist isn't started, start it with the node
+ while $node.defined {
+ my $sublist = $node.value < $locus ?? $before !! $after;
+ $sublist.last.defined ?? $sublist.last.next
+ !! $sublist.first = $node;
+ $sublist.last = $node;
+ $node = $node.next;
+ }
+
+ ## 3. link the pre list to the post list:
+ ## 3a. point the last element of the pre list to
+ ## the first element of the post
+ ## 3b. point the last element of the post list to null
+ $before.last.next = $after.first if defined $before.last;
+ $after.last.next = Nil if defined $after.last;
+
+ # output
+ $list.arrow_print();
+}
diff --git a/challenge-059/colin-crain/raku/ch-2.p6 b/challenge-059/colin-crain/raku/ch-2.p6
new file mode 100644
index 0000000000..6a9add0bfe
--- /dev/null
+++ b/challenge-059/colin-crain/raku/ch-2.p6
@@ -0,0 +1,11 @@
+sub MAIN ( *@input ) {
+
+ my @sets = @input.combinations: 2;
+ my $sum = [+] @sets.map({bit_difference($_)});
+
+ $sum.say;
+}
+
+sub bit_difference ($array) {
+ return ([+^] |$array).base(2).comb.sum;
+}