aboutsummaryrefslogtreecommitdiff
path: root/challenge-120
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-07-10 11:13:25 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-07-10 11:13:25 +0100
commitb80770db927ad7b91c1be64eba84c2a60a6b1700 (patch)
tree528f346928d639ed14381fd14a2dd9d2166e80c1 /challenge-120
parent789ade395bff1c087fc44e36b1d202eeceeb5aa9 (diff)
downloadperlweeklychallenge-club-b80770db927ad7b91c1be64eba84c2a60a6b1700.tar.gz
perlweeklychallenge-club-b80770db927ad7b91c1be64eba84c2a60a6b1700.tar.bz2
perlweeklychallenge-club-b80770db927ad7b91c1be64eba84c2a60a6b1700.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-120')
-rw-r--r--challenge-120/ulrich-rieke/haskell/ch-2.hs19
-rw-r--r--challenge-120/ulrich-rieke/perl/ch-1.pl54
-rw-r--r--challenge-120/ulrich-rieke/perl/ch-2.pl9
-rw-r--r--challenge-120/ulrich-rieke/raku/ch-1.raku35
-rw-r--r--challenge-120/ulrich-rieke/raku/ch-2.raku9
5 files changed, 126 insertions, 0 deletions
diff --git a/challenge-120/ulrich-rieke/haskell/ch-2.hs b/challenge-120/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..b711c8cde2
--- /dev/null
+++ b/challenge-120/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,19 @@
+module Challenge120_2
+ where
+import Data.List.Split( splitOn )
+
+solution :: String -> String
+solution input = show (max hoursPosition minutesPosition -
+min hoursPosition minutesPosition ) ++ " degrees"
+where
+ times :: [String]
+ times = splitOn ":" input
+ hours :: Int
+ hours = mod ( read $ head times ) 12
+ minutes :: Int
+ minutes = read $ last times
+ hoursPosition :: Double
+ hoursPosition = ( 360.0 / 12.0 ) * ( fromIntegral hours ) + (30.0 / 60.0)
+ * ( fromIntegral minutes )
+ minutesPosition :: Double
+ minutesPosition = ( 360.0 / 60.0 ) * ( fromIntegral minutes )
diff --git a/challenge-120/ulrich-rieke/perl/ch-1.pl b/challenge-120/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..d4599b4c21
--- /dev/null
+++ b/challenge-120/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub toBinaryString {
+ my $number = shift ;
+ my @bits ;
+ while ( $number != 0 ) {
+ unshift @bits , $number % 2 ;
+ $number = int ( $number / 2 ) ;
+ }
+ return join( '' , @bits ) ;
+}
+
+sub fromBinaryString {
+ my $numberstring = shift ;
+ my $sum = 0 ;
+ my @reversed = reverse split( // , $numberstring ) ;
+ my $multiplier = 1 ;
+ for my $num ( @reversed ) {
+ $sum += $multiplier * $num ;
+ $multiplier *= 2 ;
+ }
+ return $sum ;
+}
+my $N = $ARGV[ 0 ] ;
+my $numberstring = toBinaryString( $N ) ;
+my $len = length $numberstring ;
+my $firstNibble ;
+my $secondNibble ;
+if ( $len < 4 ) {
+ $firstNibble = "0" x ( 4 - $len ) . $numberstring ;
+ $secondNibble = "0" x 4 ;
+}
+if ( $len == 4 ) {
+ $firstNibble = $numberstring ;
+ $secondNibble = "0" x 4 ;
+}
+if ( $len > 4 and $len < 8 ) {
+ $firstNibble = "0" x ( 8 - $len ) . substr( $numberstring , 0 , $len - 4 ) ;
+ $secondNibble = substr( $numberstring , $len - 4 ) ;
+}
+if ( $len == 8 ) {
+ $firstNibble = substr( $numberstring , 0 , 4 ) ;
+ $secondNibble = substr( $numberstring , 4 ) ;
+}
+my $binaryString = $firstNibble . $secondNibble ;
+my $swapped ;
+for my $i ( 1 , 3 , 5 , 7 ) {
+ $swapped .= substr( $binaryString , $i , 1 ) ;
+ $swapped .= substr( $binaryString , $i - 1 , 1 ) ;
+}
+say fromBinaryString($swapped) ;
diff --git a/challenge-120/ulrich-rieke/perl/ch-2.pl b/challenge-120/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..5a8341f4b1
--- /dev/null
+++ b/challenge-120/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,9 @@
+use v6 ;
+
+sub MAIN( Str $T is copy ) {
+ my ( $hours, $minutes ) = $T.split( /':'/ ) ;
+ my $hourposition = 360 / 12 * (+$hours % 12 ) + 30 / 60 * $minutes ;
+ my $minutesposition = 360 / 60 * $minutes ;
+ say ( max( $hourposition, $minutesposition ) - min( $hourposition ,
+ $minutesposition ) ) ~ " degrees" ;
+}
diff --git a/challenge-120/ulrich-rieke/raku/ch-1.raku b/challenge-120/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..1fd749fcb6
--- /dev/null
+++ b/challenge-120/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,35 @@
+use v6 ;
+
+sub fillWithNils( Str $number is rw --> Str ) {
+ my Str $firstNibble ;
+ my Str $secondNibble ;
+ my $len = $number.chars ;
+ given $len {
+ when * < 4 {
+ $firstNibble = "0" x ( 4 - $len ) ~ $number ;
+ $secondNibble = "0" x 4 ;
+ }
+ when 4 {
+ $firstNibble = $number ;
+ $secondNibble = "0" x 4 ;
+ }
+ when * > 4 && * < 8 {
+ $firstNibble = "0" x (8 - $len ) ~ $number.substr( 0 , $len - 4 ) ;
+ $secondNibble = $number.substr( $len - 4 ) ;
+ }
+ when 8 { return $number }
+ }
+ $number = $firstNibble ~ $secondNibble ;
+ return $number ;
+}
+
+sub MAIN( Int $N is copy ) {
+ my Str $toBaseTwo = $N.base( 2 ) ;
+ my Str $filledUp = fillWithNils( $toBaseTwo ) ;
+ my Str $swapped ;
+ for (1 , 3 , 5 , 7 ) -> $i {
+ $swapped ~= $filledUp.substr( $i , 1 ) ;
+ $swapped ~= $filledUp.substr( $i - 1 , 1 ) ;
+ }
+ say $swapped.parse-base( 2 ) ;
+}
diff --git a/challenge-120/ulrich-rieke/raku/ch-2.raku b/challenge-120/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..5a8341f4b1
--- /dev/null
+++ b/challenge-120/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,9 @@
+use v6 ;
+
+sub MAIN( Str $T is copy ) {
+ my ( $hours, $minutes ) = $T.split( /':'/ ) ;
+ my $hourposition = 360 / 12 * (+$hours % 12 ) + 30 / 60 * $minutes ;
+ my $minutesposition = 360 / 60 * $minutes ;
+ say ( max( $hourposition, $minutesposition ) - min( $hourposition ,
+ $minutesposition ) ) ~ " degrees" ;
+}