aboutsummaryrefslogtreecommitdiff
path: root/challenge-104
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-03-21 07:37:23 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-03-21 07:37:23 +0000
commite9ea0d8d7f3b84d7a6c204e8fa79f91cd1df7627 (patch)
treeb0fdfb6204641d995f990c3d087c065b07370187 /challenge-104
parentd62786e4f7a410b1d5989583d530b2175e4ed749 (diff)
downloadperlweeklychallenge-club-e9ea0d8d7f3b84d7a6c204e8fa79f91cd1df7627.tar.gz
perlweeklychallenge-club-e9ea0d8d7f3b84d7a6c204e8fa79f91cd1df7627.tar.bz2
perlweeklychallenge-club-e9ea0d8d7f3b84d7a6c204e8fa79f91cd1df7627.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-104')
-rw-r--r--challenge-104/ulrich-rieke/cpp/ch-1.cpp22
-rw-r--r--challenge-104/ulrich-rieke/haskell/ch-1.hs14
-rw-r--r--challenge-104/ulrich-rieke/lisp/ch-1.lisp12
-rw-r--r--challenge-104/ulrich-rieke/perl/ch-1.pl17
-rw-r--r--challenge-104/ulrich-rieke/perl/ch-2.pl88
-rw-r--r--challenge-104/ulrich-rieke/raku/ch-1.raku14
-rw-r--r--challenge-104/ulrich-rieke/raku/ch-2.raku87
7 files changed, 254 insertions, 0 deletions
diff --git a/challenge-104/ulrich-rieke/cpp/ch-1.cpp b/challenge-104/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..f4a8be6fc5
--- /dev/null
+++ b/challenge-104/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,22 @@
+#include <vector>
+#include <iostream>
+
+int main( ) {
+ std::vector<int> fusc ;
+ fusc.push_back( 0 ) ;
+ fusc.push_back( 1 ) ;
+ for ( int n = 2 ; n < 50 ; n++ ) {
+ if ( n % 2 == 0 ) {
+ fusc.push_back( fusc[ n / 2 ] ) ;
+ }
+ else {
+ int half = n / 2 ;
+ fusc.push_back( fusc[ half ] + fusc[ half + 1 ] ) ;
+ }
+ }
+ for ( int i : fusc ) {
+ std::cout << i << " " ;
+ }
+ std::cout << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-104/ulrich-rieke/haskell/ch-1.hs b/challenge-104/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..3e8d855346
--- /dev/null
+++ b/challenge-104/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,14 @@
+module Challenge104
+ where
+import Data.List ( (!!) )
+
+myFunc :: (Int, [Int]) -> (Int , [Int] )
+myFunc ( n , list )
+ |even (n + 1 ) = (n + 1 , list ++ [list !! half])
+ |otherwise = (n + 1 , list ++ [ (list !! half ) + (list !! (half + 1 ) ) ] )
+ where
+ half :: Int
+ half = div (n + 1 ) 2
+
+solution :: [Int]
+solution = snd $ last $ take 49 $ iterate myFunc ( 1 , [0,1])
diff --git a/challenge-104/ulrich-rieke/lisp/ch-1.lisp b/challenge-104/ulrich-rieke/lisp/ch-1.lisp
new file mode 100644
index 0000000000..e02e1c1a46
--- /dev/null
+++ b/challenge-104/ulrich-rieke/lisp/ch-1.lisp
@@ -0,0 +1,12 @@
+#!/usr/bin/sbcl --script
+
+(defun theNumbers ( )
+(let ((nums (list 0 1)))
+ (dotimes (j 48)
+ (if (evenp ( + 2 j ))
+ (setf nums ( append nums ( list (elt nums (/(+ 2 j) 2) ) )))
+ (setf nums ( append nums ( list (+ (elt nums (floor (/ (+ 2 j ) 2 )) )
+ (elt nums (+ (floor (/ ( + 2 j ) 2 )) 1) )))))))
+ ( write nums )))
+
+(theNumbers)
diff --git a/challenge-104/ulrich-rieke/perl/ch-1.pl b/challenge-104/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..b3bc2b3884
--- /dev/null
+++ b/challenge-104/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+
+my @fusc ;
+push (@fusc , 0 , 1 ) ;
+for my $n (2 .. 49 ) {
+ if ( $n % 2 == 0 ) {
+ $fusc[ $n ] = $fusc[ $n / 2 ] ;
+ }
+ else {
+ my $half = int( $n / 2 ) ;
+ $fusc[ $n ] = $fusc[ $half ] + $fusc[ $half + 1 ] ;
+ }
+}
+map { print "$_ "} @fusc ;
+print "\n" ;
diff --git a/challenge-104/ulrich-rieke/perl/ch-2.pl b/challenge-104/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..e1d49245bc
--- /dev/null
+++ b/challenge-104/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+#it more or less a translation of the Raku solution, assume the comments
+#to be added here
+
+sub humandraw {
+ my $parts = shift ;
+ say "How many pieces do you want to take ?" ;
+ my $takeaway = <STDIN> ;
+ chomp $takeaway ;
+ while ( $takeaway > 3 ) {
+ say "don't take more than 3 pieces!" ;
+ $takeaway = <STDIN> ;
+ chomp $takeaway ;
+ }
+ $parts -= $takeaway ;
+ say "We now have $parts pieces!" ;
+ return $parts ;
+}
+
+sub computerdraw {
+ my $firstDraw = shift ;
+ my $parts = shift ;
+ my $takeaway ;
+ if ( $firstDraw ) {
+ $takeaway = 2 ;
+ }
+ else {
+ if ( $parts > 8 and $parts < 12 ) {
+ $takeaway = $parts - 8 ;
+ }
+ if ( $parts > 4 and $parts < 8 ) {
+ $takeaway = $parts - 4 ;
+ }
+ if ( $parts == 4 ) {
+ $takeaway = 1 ;
+ }
+ if ( $parts == 8 ) {
+ $takeaway = 1 ;
+ }
+ if ( $parts > 0 and $parts < 4 ) {
+ $takeaway = $parts ;
+ }
+ }
+ $parts -= $takeaway ;
+ say "I took $takeaway piece(s)!" ;
+ say "We now have $parts piece(s)!" ;
+ return $parts ;
+}
+
+my $pieces = 12 ;
+my $firstDraw = 1 ;
+my $num = rand ;
+if ( $num < 0.5 ) {
+ say "You start!" ;
+ $firstDraw = 0 ;
+ while ( $pieces > 0 ) {
+ $pieces = humandraw( $pieces ) ;
+ if ( $pieces == 0 ) {
+ say "You won!" ;
+ last ;
+ }
+ $pieces = computerdraw( $firstDraw , $pieces ) ;
+ if ( $pieces == 0 ) {
+ say "I won!" ;
+ last ;
+ }
+ }
+}
+else {
+ say "I start!" ;
+ while ( $pieces > 0 ) {
+ $pieces = computerdraw( $firstDraw, $pieces ) ;
+ $firstDraw = 0 ;
+ if ( $pieces == 0 ) {
+ say "I won!" ;
+ last ;
+ }
+ $pieces = humandraw( $pieces ) ;
+ if ( $pieces == 0 ) {
+ say "You won!" ;
+ last ;
+ }
+ }
+}
diff --git a/challenge-104/ulrich-rieke/raku/ch-1.raku b/challenge-104/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..87eb6efb99
--- /dev/null
+++ b/challenge-104/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,14 @@
+use v6 ;
+
+my @fusc ;
+@fusc.push( 0 , 1 ) ;
+for (2 .. 49 ) -> $n {
+ if ( $n %% 2 ) {
+ @fusc[ $n ] = @fusc[ $n div 2 ] ;
+ }
+ else {
+ my $half = $n div 2 ;
+ @fusc[ $n ] = @fusc[ $half ] + @fusc[ $half + 1 ] ;
+ }
+}
+say @fusc ;
diff --git a/challenge-104/ulrich-rieke/raku/ch-2.raku b/challenge-104/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..fcbab40391
--- /dev/null
+++ b/challenge-104/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,87 @@
+use v6 ;
+
+#we do not assume a full-fledged Nim game with a number of different rows but
+#rather a one-row game with 12 pieces according to the given task ( the number
+#of combinations would very much depend on the number of rows, the pieces in
+#them and the constraint "draw all pieces from one row"
+
+#in humandraw, we assume some amount of naivete....
+sub humandraw( Int $parts is rw ) {
+ my $takeaway = prompt( "How many pieces do you want to take ? " ) ;
+ while ( $takeaway > 3 ) {
+ say "don't take more than 3 pieces!" ;
+ $takeaway = prompt( "How many pieces do you want to take ?" ) ;
+ }
+ $parts -= $takeaway ;
+ say "We now have $parts pieces!" ;
+ return $parts ;
+}
+
+#we force the opposition to end with (12 - ( 3 + 1 )) so it can't win
+#we should end up at (12 - ( 3 + 1 ) - 4 * n ) so that the opposition
+#can't win( reference: Wikipedia about Bachet's game )
+sub computerdraw( Bool $firstDraw , Int $parts is rw ) {
+ my $takeaway ;
+ if ( $firstDraw ) {#we probably can't stop the opposition from reaching 4 so
+#in this case $takeaway doesn't really matter
+ $takeaway = 2 ;
+ }
+ else {
+ if ( 8 < $parts < 12) {
+ $takeaway = $parts - 8 ;
+ }
+ if ( 4 < $parts < 8 ) {
+ $takeaway = $parts - 4 ;
+ }
+ if ( $parts == 4 ) {#we are lost anyway
+ $takeaway = (1,2,3).pick ;
+ }
+ if ( $parts == 8 ) {
+ $takeaway = 1 ;
+ }
+ if ( 0 < $parts < 4 ) {
+ $takeaway = $parts ;
+ }
+ }
+ $parts -= $takeaway ;
+ say "I took $takeaway piece(s)!" ;
+ say "We now have $parts pieces!" ;
+ return $parts ;
+}
+
+my $pieces = 12 ;
+my Bool $firstDraw = True ;
+#who is to start ?
+my $number = (0,1).pick( 1 ) ;
+if ( $number == 0 ) {
+ say "You start!" ;
+ $firstDraw = False ;
+ while ( $pieces > 0 ) {
+ $pieces = humandraw( $pieces ) ;
+ if ( $pieces == 0 ) {
+ say "You won!" ;
+ last ;
+ }
+ $pieces = computerdraw( $firstDraw , $pieces ) ;
+ if ( $pieces == 0 ) {
+ say "I won!" ;
+ last ;
+ }
+ }
+}
+else {
+ say "I start!" ;
+ while ( $pieces > 0 ) {
+ $pieces = computerdraw( $firstDraw , $pieces ) ;
+ $firstDraw = False ;
+ if ( $pieces == 0 ) {
+ say "I won!" ;
+ last ;
+ }
+ $pieces = humandraw( $pieces ) ;
+ if ( $pieces == 0 ) {
+ say "You won!" ;
+ last ;
+ }
+ }
+}