diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-09 09:04:03 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-09 09:04:03 +0100 |
| commit | de0ca0b00cad07ea6ec6a30d3b12207efd1e55e7 (patch) | |
| tree | a5254b6a4b0d08c6493b090a4b4ef34304cdec2e /challenge-059/colin-crain/perl | |
| parent | d67e3e2c60369a2c33e42a215374f907a24cc19d (diff) | |
| download | perlweeklychallenge-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/perl')
| -rw-r--r-- | challenge-059/colin-crain/perl/ch-1.pl | 88 | ||||
| -rw-r--r-- | challenge-059/colin-crain/perl/ch-2.pl | 35 |
2 files changed, 123 insertions, 0 deletions
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; +} |
