aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
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
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')
-rw-r--r--challenge-059/colin-crain/blog.txt1
-rw-r--r--challenge-059/colin-crain/perl/ch-1.pl88
-rw-r--r--challenge-059/colin-crain/perl/ch-2.pl35
-rw-r--r--challenge-059/colin-crain/raku/ch-1.p665
-rw-r--r--challenge-059/colin-crain/raku/ch-2.p611
5 files changed, 200 insertions, 0 deletions
diff --git a/challenge-059/colin-crain/blog.txt b/challenge-059/colin-crain/blog.txt
new file mode 100644
index 0000000000..196a8bde29
--- /dev/null
+++ b/challenge-059/colin-crain/blog.txt
@@ -0,0 +1 @@
+https://colincrain.wordpress.com/2020/05/08/sorting-a-linked-list-and-summing-binary-bits/
diff --git a/challenge-059/colin-crain/perl/ch-1.pl b/challenge-059/colin-crain/perl/ch-1.pl
new file mode 100644
index 0000000000..ee71fb31ae
--- /dev/null
+++ b/challenge-059/colin-crain/perl/ch-1.pl
@@ -0,0 +1,88 @@
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN:
+
+## acquire the locus value and the array representation
+## of the linked list
+my ($locus, @input) = @ARGV;
+my $next = undef;
+my $node;
+
+## 1. convert the input commandline array into a linked list
+while (scalar @input > 0) {
+ my $value = pop @input;
+ $node = new Node($value, $next);
+ $next = $node
+}
+## $node currently points to beginning of the list
+
+my $prelist_first;
+my $prelist_last;
+my $postlist_first;
+my $postlist_last;
+
+while (defined $node) {
+ ## 2a. if it is less than the given value, add it to
+ ## the end of the pre list
+ if ($node->value < $locus) {
+ defined $prelist_last ? $prelist_last->next($node)
+ : ($prelist_first = $node);
+ $prelist_last = $node;
+ }
+ ## 2b. if it is more than or equal to the given value
+ ## add it to the end of the post list
+ else {
+ defined $postlist_last ? $postlist_last->next($node)
+ : ($postlist_first = $node);
+ $postlist_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
+$prelist_last->next($postlist_first) if (defined $prelist_last);
+
+## 3b. point the last element of the post list to null
+$postlist_last->{'next'} = undef;
+
+## ## ## output
+
+## if prelist never got made, start with the postlist
+$node = $prelist_first || $postlist_first;
+my @output;
+while (defined $node) {
+ push @output, $node->value;
+ $node = $node->next;
+}
+say join ' → ', @output;
+
+
+## ## ## ## ## NODE PACKAGE
+
+package Node;
+
+sub new {
+ my ($class, $value, $next) = @_;
+ my $self = { "value" => $value,
+ "next" => $next };
+ bless $self, $class;
+ return $self;
+}
+
+sub value {
+ my ($self, $value ) = @_;
+ $self->{value} = $value if defined $value;
+ return $self->{value}
+}
+
+sub next {
+ my ($self, $next ) = @_;
+ $self->{next} = $next if defined $next;
+ return $self->{next}
+}
diff --git a/challenge-059/colin-crain/perl/ch-2.pl b/challenge-059/colin-crain/perl/ch-2.pl
new file mode 100644
index 0000000000..7f9768a6f2
--- /dev/null
+++ b/challenge-059/colin-crain/perl/ch-2.pl
@@ -0,0 +1,35 @@
+use warnings;
+use strict;
+use feature ":5.26";
+
+use List::Util qw(sum);
+
+## ## ## ## ## MAIN:
+
+my @array = @ARGV;
+my @sets = choose_2_sets( @array );
+
+my $sum;
+for my $set ( @sets ) {
+ $sum += bit_difference($set->[0], $set->[1]);
+}
+
+say $sum;
+
+
+## ## ## ## ## SUBS:
+
+sub bit_difference {
+ return sum( split //, sprintf "%b", 0+$_[0] ^ 0+$_[1] );
+}
+
+sub choose_2_sets {
+ my @array = @_;
+ my @out;
+ for my $i (0..(scalar @array - 1)) {
+ for my $j ($i+1..(scalar @array - 1)) {
+ push @out, [ $array[$i], $array[$j] ];
+ }
+ }
+ return @out;
+}
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;
+}