aboutsummaryrefslogtreecommitdiff
path: root/challenge-120
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-120')
-rw-r--r--challenge-120/pete-houston/perl/ch-1.pl24
-rw-r--r--challenge-120/pete-houston/perl/ch-2.pl46
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-120/pete-houston/perl/ch-1.pl b/challenge-120/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..9c0862db3e
--- /dev/null
+++ b/challenge-120/pete-houston/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 12001.pl
+#
+# USAGE: ./12001.pl N
+#
+# DESCRIPTION: Swap the odd bits with the even ones.
+#
+# REQUIREMENTS: N must be an integer between 0 and 255 inclusive
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 05/07/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+my $n = shift;
+my %masks = ( even => 0b10101010, odd => 0b01010101 );
+my $swapped = ($n & $masks{even}) / 2 + ($n & $masks{odd}) * 2;
+
+print "$swapped\n";
diff --git a/challenge-120/pete-houston/perl/ch-2.pl b/challenge-120/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..36c22fceab
--- /dev/null
+++ b/challenge-120/pete-houston/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 12002.pl
+#
+# USAGE: ./12002.pl HH:MM
+#
+# DESCRIPTION: Print the non-reflex angle in degrees between the clock hands
+#
+# REQUIREMENTS: IO::All and a UTF-8 compatible terminal
+# NOTES: There is AFAICT no term for an angle less than pi.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 05/07/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+use IO::All -utf8;
+
+my ($h, $m) = get_input ();
+
+my $minfrac = $m / 60;
+my $hfrac = ($h + $minfrac) / 12;
+my $fracdiff = abs ($hfrac - $minfrac);
+
+# Ensure we are using the non-reflex angle
+$fracdiff = 1 - $fracdiff if $fracdiff > 0.5;
+
+# Convert fraction into angle
+my $diffang = $fracdiff * 360;
+my $deg = int $diffang;
+my $min = ($diffang - $deg) * 60;
+
+my $fmt = "At %2.2i:%2.2i, the angle between the hands is " .
+ "%i\N{DEGREE SIGN} %.0f'\n";
+printf $fmt, $h, $m, $deg, $min;
+
+sub get_input {
+ my $in = shift @ARGV;
+ $in =~ /^([012]?[0-9]):([0-5][0-9])$/ or
+ die "'$in' is not a valid time in HH:MM format.\n";
+ return ($1 % 12, $2);
+}