aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-119/mattneleigh/README1
-rwxr-xr-xchallenge-119/mattneleigh/perl/ch-1.pl29
-rwxr-xr-xchallenge-119/mattneleigh/perl/ch-2.pl56
-rw-r--r--challenge-120/mattneleigh/README1
-rwxr-xr-xchallenge-120/mattneleigh/perl/ch-1.pl29
-rwxr-xr-xchallenge-120/mattneleigh/perl/ch-2.pl44
-rw-r--r--challenge-121/mattneleigh/README1
-rwxr-xr-xchallenge-121/mattneleigh/perl/ch-1.pl55
-rwxr-xr-xchallenge-121/mattneleigh/perl/ch-2.pl198
9 files changed, 414 insertions, 0 deletions
diff --git a/challenge-119/mattneleigh/README b/challenge-119/mattneleigh/README
new file mode 100644
index 0000000000..489bdfb8fe
--- /dev/null
+++ b/challenge-119/mattneleigh/README
@@ -0,0 +1 @@
+Solution by Matthew Neleigh
diff --git a/challenge-119/mattneleigh/perl/ch-1.pl b/challenge-119/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..4cfe4495c5
--- /dev/null
+++ b/challenge-119/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my @args = (101, 18);
+
+foreach(@args){
+ printf("%4d -> %4d\n", $_, swap_nibbles($_));
+}
+
+exit(0);
+
+
+
+sub swap_nibbles{
+
+ return(
+ # New most significant nibble
+ (($_[0] << 4) & 0xF0)
+ |
+ # New least significant nibble
+ (($_[0] >> 4) & 0x0F)
+ );
+
+}
+
+
+
diff --git a/challenge-119/mattneleigh/perl/ch-2.pl b/challenge-119/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..2bdc6f2627
--- /dev/null
+++ b/challenge-119/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+my @args = (5, 10, 60);
+my @answers;
+
+# Sort args just for general safety-
+# without_one_on_one() expects a
+# sorted list
+@args = sort({$a <=> $b} @args);
+
+@answers = without_one_on_one(@args);
+
+foreach(@args){
+ printf("%4d -> %4d\n", $_, shift(@answers));
+}
+
+exit(0);
+
+
+
+sub without_one_on_one{
+
+ my $i = 1;
+ my $num = 1;
+ my @sequence = ();
+
+ while(1){
+ # Treat the number like a string
+ # because we can
+ if($num =~ m/^[123]+$/ && $num !~ m/11/){
+ # Digit sequence matches criteria
+ if($i == $ARG[0]){
+ # This is the 0th member of the
+ # remaining sequence- store it
+ push(@sequence, $num);
+
+ # Strip the 0th element from @ARG and
+ # break the loop if @ARG is empty
+ shift(@ARG);
+ last unless(@ARG);
+ }
+ $i++;
+ }
+ $num++;
+ }
+
+ return(@sequence);
+
+}
+
+
+
diff --git a/challenge-120/mattneleigh/README b/challenge-120/mattneleigh/README
new file mode 100644
index 0000000000..489bdfb8fe
--- /dev/null
+++ b/challenge-120/mattneleigh/README
@@ -0,0 +1 @@
+Solution by Matthew Neleigh
diff --git a/challenge-120/mattneleigh/perl/ch-1.pl b/challenge-120/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..30a91ee445
--- /dev/null
+++ b/challenge-120/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my @args = (101, 18);
+
+foreach(@args){
+ printf("%4d -> %4d\n", $_, switch_bits($_));
+}
+
+exit(0);
+
+
+
+sub switch_bits{
+
+ return(
+ # Bits raised
+ (($_[0] << 1) & 0xAA)
+ |
+ # Bits lowered
+ (($_[0] >> 1) & 0x55)
+ );
+
+}
+
+
+
diff --git a/challenge-120/mattneleigh/perl/ch-2.pl b/challenge-120/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..b18f25cfbe
--- /dev/null
+++ b/challenge-120/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my @args = ("03:10", "04:00", "12:55", "06:30", "01:05", "01:07");
+
+foreach(@args){
+ printf("%5s -> %4d\n", $_, clock_angle($_));
+}
+
+exit(0);
+
+
+
+sub clock_angle{
+
+ my $angle;
+ my $hour;
+ my $minute;
+
+ # Extract hours and minutes from the
+ # first argument
+ ($hour, $minute) = split(':', $_[0]);
+ $hour %= 12;
+
+ # Calculate difference between the
+ # positions of the minute hand and hour
+ # hand, taking into account the
+ # latter's motion past the top of the
+ # hour
+ $angle = abs(5.5 * $minute - 30 * $hour);
+
+ # Make sure we report the minor angle
+ if($angle > 180){
+ $angle = 360 - $angle;
+ }
+
+ return($angle);
+
+}
+
+
+
diff --git a/challenge-121/mattneleigh/README b/challenge-121/mattneleigh/README
new file mode 100644
index 0000000000..489bdfb8fe
--- /dev/null
+++ b/challenge-121/mattneleigh/README
@@ -0,0 +1 @@
+Solution by Matthew Neleigh
diff --git a/challenge-121/mattneleigh/perl/ch-1.pl b/challenge-121/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..58291250ac
--- /dev/null
+++ b/challenge-121/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @args = (
+ [ 12, 3 ],
+ [ 18, 4 ]
+);
+
+foreach(@args){
+ printf(
+ "m = %3d n = %1d -> %3d\n",
+ $_->[0], $_->[1], invert_bit(@{$_}));
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Invert the specified bit in an integer between 0 and 255; that is to say, if
+# the bit is 1, make it 0, and vice versa
+# Takes two arguments:
+# * The integer (M) to modify
+# * The bit (N; 1-8) to be inverted
+# Returns on success:
+# * The modified integer
+# Returns on error:
+# * undef if M is not between 0 and 255, inclusive, or N is not between 1 and
+# 8, inclusive
+################################################################################
+sub invert_bit{
+ my $m = shift();
+ my $n = shift();
+
+ unless($m > -1 && $m < 256 && $n > 0 && $n < 9){
+ return(undef);
+ }
+
+ return(
+ $m ^ (0x01 << ($n - 1))
+ );
+
+}
+
+
+
diff --git a/challenge-121/mattneleigh/perl/ch-2.pl b/challenge-121/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..51fade642f
--- /dev/null
+++ b/challenge-121/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,198 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @args = (
+ # Specified mileage grid
+ [
+ [0, 5, 2, 7],
+ [5, 0, 5, 3],
+ [3, 1, 0, 6],
+ [4, 5, 4, 0]
+ ],
+
+ # Extra credit- large random mileage grids
+ # of 15x15 and 20x20
+ create_random_grid(15, 9),
+ create_random_grid(20, 9)
+);
+my $itinerary;
+my $grid;
+
+foreach $grid (@args){
+ print_mileage_grid($grid);
+ $itinerary = plan_sales_route($grid, 0);
+ printf("length = %d\ttour = (%s)\n",
+ $itinerary->[0], join(" ", @{$itinerary->[1]})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Produce a salesperson's travel itinerary and compute the total distance
+# traveled
+# Takes two arguments:
+# * A reference to a grid (2D array of dimensions NxN; N need not be specified
+# here) of mileages between cities
+# * The index of the city in which the salesperson is to begin (and end) their
+# tour
+# Returns:
+# * A ref to an array whose first element is the total distance to be traveled,
+# and whose second element is a ref to an array of indicies, taken from the
+# supplied grid of cities to be visited, that describes the calculated
+# itinerary, e.g.:
+# $itinerary = [ 10, [0, 2, 1, 3, 0] ]
+################################################################################
+sub plan_sales_route{
+ my $grid = shift();
+ my $start = shift();
+
+ my $i;
+ my $j;
+ my @visited;
+ my $distance;
+ my $city;
+ my $itinerary = [
+ 0,
+ []
+ ];
+
+ # Initialize a few things
+ $j = scalar(@{$grid->[0]});
+ $i = $j - 1;
+ while($j--){
+ $visited[$j] = 0;
+ }
+
+ # Start at the specified city- add
+ # the city to the tour itinerary and
+ # mark it as having been visited
+ push(@{$itinerary->[1]}, $start);
+ $visited[$start] = 1;
+
+ # Take a tour of predetermined length
+ while($i--){
+ # Presumably no city will be a
+ # million miles away...
+ $distance = 1000000;
+ $city = 0;
+
+ # Check all the cities we can get to
+ # from the current city
+ for($j=0; $j<scalar(@{$grid->[0]}); $j++){
+ # Skip this potential next city if
+ # we've visited already
+ next if($visited[$j]);
+
+ if($grid->[$itinerary->[1][-1]][$j] < $distance){
+ # The mileage to this city is lower
+ # than any previously examined
+ # segment; make a note of it
+ $distance = $grid->[$itinerary->[1][-1]][$j];
+ $city = $j;
+ }
+ }
+
+ # Add mileage from the next segment to
+ # the total, add details of the next
+ # segment to the itinerary, and mark
+ # the next city as visited
+ $itinerary->[0] += $distance;
+ push(@{$itinerary->[1]}, $city);
+ $visited[$city] = 1;
+ }
+
+ # Return to the specified city: add the
+ # mileage between the last city visited
+ # and the origin city, and put the
+ # origin city in the tour itinerary again
+ $itinerary->[0] += $grid->[
+ $itinerary->[1][-1]
+ ]
+ [$start];
+ push(@{$itinerary->[1]}, $start);
+
+ return($itinerary);
+
+}
+
+
+
+################################################################################
+# Create a random mileage grid
+# Takes two arguments:
+# * The number of cities to include in the grid (N)
+# * The maximum distance allowed between any two cities
+# Returns:
+# * A ref to a populated mileage grid- a 2D array with dimensions NxN
+################################################################################
+sub create_random_grid{
+ my $n = shift();
+ my $max = shift();
+
+ my $grid;
+ my $i;
+ my $j;
+
+ $max = int($max);
+
+ # Loop over both coordinates; this makes
+ # heavy (ab)use of autovivification
+ $i = $n;
+ while($i--){
+ $j = $n;
+ while($j--){
+ # If the two coordinates are equal,
+ # store zero, otherwise store a random
+ # (but nonzero) integer
+ $grid->[$i][$j] = $i == $j ? 0 : int(rand($max)) + 1;
+ }
+ }
+
+ return($grid);
+
+}
+
+
+
+################################################################################
+# Print the contents of a mileage grid to STDOUT
+# Takes one argument:
+# * A reference to a mileage grid (2D array of dimensions NxN; N need not be
+# specified here)
+# Returns:
+# * The same mileage grid reference passed as the argument, in case the caller
+# wishes to produce output en passant while creating grids
+################################################################################
+sub print_mileage_grid{
+ my $grid = shift();
+
+ my $i;
+
+ print("[\n");
+ for($i=0; $i<scalar(@{$grid}); $i++){
+ printf(
+ " [ %s ]%s\n",
+ join(", ", @{$grid->[$i]}),
+ $i == $#{$grid} ? "" : ","
+ );
+ }
+ print("]\n");
+
+ return($grid);
+
+}
+
+
+