aboutsummaryrefslogtreecommitdiff
path: root/challenge-152
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-152')
-rw-r--r--challenge-152/ulrich-rieke/cpp/ch-1.cpp37
-rw-r--r--challenge-152/ulrich-rieke/cpp/ch-2.cpp52
-rw-r--r--challenge-152/ulrich-rieke/haskell/ch-1.hs11
-rw-r--r--challenge-152/ulrich-rieke/haskell/ch-2.hs40
-rw-r--r--challenge-152/ulrich-rieke/perl/ch-1.pl34
-rw-r--r--challenge-152/ulrich-rieke/perl/ch-2.pl68
-rw-r--r--challenge-152/ulrich-rieke/raku/ch-1.raku27
-rw-r--r--challenge-152/ulrich-rieke/raku/ch-2.raku36
8 files changed, 305 insertions, 0 deletions
diff --git a/challenge-152/ulrich-rieke/cpp/ch-1.cpp b/challenge-152/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..882325fb71
--- /dev/null
+++ b/challenge-152/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,37 @@
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+int main( ) {
+ std::vector<std::vector<int>> triangle ;
+ std::vector<int> row ;
+ int rows = 0 ;
+ std::cout << "How many rows do you want to enter?" ;
+ std::cin >> rows ;
+ int number = 0 ;
+ int howmany = 1 ;
+ for ( int i = 0 ; i < rows; i++ ) {
+ std::cout << howmany << " numbers for row " << (i + 1) << '\n' ;
+ if ( howmany == 1 ) {
+ std::cin >> number ;
+ row.push_back( number ) ;
+ triangle.push_back( row ) ;
+ row.clear( ) ;
+ }
+ else {
+ for ( int j = 0 ; j < howmany ; j++ ) {
+ std::cin >> number ;
+ row.push_back( number ) ;
+ }
+ triangle.push_back( row ) ;
+ row.clear( ) ;
+ }
+ howmany++ ;
+ }
+ int pathsum = 0 ;
+ for ( auto & aRow : triangle )
+ pathsum += *(std::min_element( aRow.begin( ) , aRow.end( ))) ;
+ std::cout << std::endl ;
+ std::cout << pathsum << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-152/ulrich-rieke/cpp/ch-2.cpp b/challenge-152/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..da19048dda
--- /dev/null
+++ b/challenge-152/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,52 @@
+#include <iostream>
+#include <vector>
+#include <cstdlib>
+#include <algorithm>
+#include <iterator>
+
+int main( ) {
+ std::cout << "Please enter four integers denoting the lower left and upper right point" ;
+ std::cout << "\nof the first rectangle!\n" ;
+ int num = 0 ;
+ std::vector<int> rectangle1 ;
+ for ( int i = 0 ; i < 4 ; i++ ) {
+ std::cin >> num ;
+ rectangle1.push_back( num ) ;
+ }
+ std::vector<int> rectangle2 ;
+ std::cout << "and now enter four integers for the second rectangle!\n" ;
+ num = 0 ;
+ for ( int i = 0 ; i < 4 ; i++ ) {
+ std::cin >> num ;
+ rectangle2.push_back( num ) ;
+ }
+ int firstArea = (*(rectangle1.begin( ) + 2) - *(rectangle1.begin( ))) *
+ (*(rectangle1.begin( ) + 3) - *(rectangle1.begin( ) + 1 )) ;
+ int secondArea = (*(rectangle2.begin( ) + 2) - *(rectangle2.begin( ))) *
+ (*(rectangle2.begin( ) + 3) - *(rectangle2.begin( ) + 1 )) ;
+ std::vector<int> first_xes ;
+ std::vector<int> second_xes ;
+ std::vector<int> first_ys ;
+ std::vector<int> second_ys ;
+ for ( int i = rectangle1[0] ; i < rectangle1[2] + 1 ; i++ )
+ first_xes.push_back( i ) ;
+ for ( int i = rectangle1[1] ; i < rectangle1[3] + 1 ; i++ )
+ first_ys.push_back( i ) ;
+ for ( int i = rectangle2[0] ; i < rectangle2[2] + 1 ; i++ )
+ second_xes.push_back( i ) ;
+ for ( int i = rectangle2[1] ; i < rectangle2[3] + 1 ; i++ )
+ second_ys.push_back( i ) ;
+ std::vector<int> commonX ;
+ std::set_intersection( first_xes.begin( ) , first_xes.end( ) , second_xes.begin( ) ,
+ second_xes.end( ) , std::inserter( commonX ,commonX.begin( ))) ;
+ std::vector<int> commonY ;
+ std::set_intersection( first_ys.begin( ) , first_ys.end( ) , second_ys.begin( ) ,
+ second_ys.end( ) , std::inserter( commonY , commonY.begin( ))) ;
+ int commonArea = (*std::max_element( commonX.begin( ) , commonX.end( )) -
+ *std::min_element( commonX.begin( ) , commonX.end( ) ) ) *
+ (*std::max_element( commonY.begin( ) , commonY.end( ) ) -
+ *std::min_element( commonY.begin( ) , commonY.end( )) ) ;
+ std::cout << std::endl ;
+ std::cout << (firstArea + secondArea - commonArea) << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-152/ulrich-rieke/haskell/ch-1.hs b/challenge-152/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..09d4cfa572
--- /dev/null
+++ b/challenge-152/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,11 @@
+module Challenge152
+ where
+import Data.List.Split( divvy )
+
+valid :: [[Int]] -> Bool
+valid input = all(\l -> abs( (length $ head l) - (length $ last l)) == 1 )
+$ divvy 2 1 input
+
+solution :: [[Int]] -> Int
+solution list = if not $ valid list then error "One more number per row" else
+sum $ map minimum list
diff --git a/challenge-152/ulrich-rieke/haskell/ch-2.hs b/challenge-152/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..c44e4bbb7b
--- /dev/null
+++ b/challenge-152/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,40 @@
+module Challenge152_2
+ where
+import qualified Data.Set as S
+import Data.List.Split( splitOn )
+import Data.List ( (!!) )
+
+computeArea :: (Int , Int) -> (Int , Int) -> Int
+computeArea lowerLeft upperRight = ( fst upperRight - fst lowerLeft ) *
+( snd upperRight - snd lowerLeft )
+
+solution :: (Int , Int) -> (Int , Int) -> (Int , Int) -> (Int , Int) -> Int
+solution firstLowerLeft firstUpperRight secondLowerLeft secondUpperRight =
+computeArea firstLowerLeft firstUpperRight + computeArea secondLowerLeft
+secondUpperRight - commonArea
+where
+ commonX :: [Int]
+ commonX = S.toList $ S.fromList (enumFromTo (fst firstLowerLeft)
+ (fst firstUpperRight)) `S.intersection` S.fromList ( enumFromTo
+ (fst secondLowerLeft) (fst secondUpperRight) )
+ commonY :: [Int]
+ commonY = S.toList $ S.fromList (enumFromTo (snd firstLowerLeft)
+ (snd firstUpperRight)) `S.intersection` S.fromList ( enumFromTo
+ (snd secondLowerLeft) (snd secondUpperRight) )
+ commonArea :: Int
+ commonArea = ( maximum commonX - minimum commonX ) * ( maximum commonY -
+ minimum commonY )
+
+main :: IO ( )
+main = do
+ putStrLn "Please enter lower left and upper right point of first rectangle!"
+ putStrLn "Enter integers separated by spaces!"
+ rect1 <- getLine
+ putStrLn "Please enter lower left and upper right point of second rectangle!"
+ putStrLn "Enter integers separated by spaces!"
+ rect2 <- getLine
+ let rect1Nums = fmap read $ splitOn " " rect1
+ rect2Nums = fmap read $ splitOn " " rect2
+ putStrLn $ show $ solution ( rect1Nums !! 0 , rect1Nums !! 1 ) ( rect1Nums !! 2 ,
+ rect1Nums !! 3 ) ( rect2Nums !! 0 , rect2Nums !! 1 ) ( rect2Nums !! 2 ,
+ rect2Nums !! 3 )
diff --git a/challenge-152/ulrich-rieke/perl/ch-1.pl b/challenge-152/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..cf1aeb3976
--- /dev/null
+++ b/challenge-152/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw( min ) ;
+
+say "Enter integers separated by spaces! Enter e to end data entry!" ;
+say "line by line, enter one more number than in the previous line!" ;
+my $lastLen = 0 ;
+my $len ;
+my $pathSum = 0 ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line ne "e" ) {
+ while ( $line !~ /^(\d\s*)+$/ ) {
+ say "Enter only integers separated by spaces, one more per line!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ }
+ unless ( $line eq "e" ) {
+ $len = scalar( split( /\s+/ , $line ) ) ;
+ while ( $len != $lastLen + 1 ) {
+ say "There should be one number more per line!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ $len = scalar( split( /\s+/ , $line ) ) ;
+ }
+ }
+ $pathSum += min( split( /\s+/ , $line )) ;
+ $lastLen = $len ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+say $pathSum ;
diff --git a/challenge-152/ulrich-rieke/perl/ch-2.pl b/challenge-152/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..9c5589722a
--- /dev/null
+++ b/challenge-152/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( max min ) ;
+
+say "Enter 4 integers per rectangle to denote the lower left and upper right";
+say "corner of the rectangles! Separate numbers by spaces!" ;
+say "rectangle 1 =>" ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line !~ /^([+-]*\d+\s*)+$/ ) {
+ say "please enter 4 numbers separated by spaces!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+my @firstNumbers = split( /\s+/ , $line ) ;
+while ( scalar( @firstNumbers ) != 4 ) {
+ say "Enter 4 numbers separated by spaces!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ @firstNumbers = split( /\s+/ , $line ) ;
+}
+say "rectangle 2 =>" ;
+$line = <STDIN> ;
+chomp $line ;
+while ( $line !~ /^([+-]*\d+\s*)+$/ ) {
+ say "please enter 4 numbers separated by spaces!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+my @secondNumbers = split( /\s+/ , $line ) ;
+while ( scalar( @secondNumbers ) != 4 ) {
+ say "Enter 4 numbers separated by spaces!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ @secondNumbers = split( /\s+/ , $line ) ;
+}
+my %firstXNumbers ;
+my %firstYNumbers ;
+my %secondXNumbers ;
+my %secondYNumbers ;
+for my $i ( $firstNumbers[0] .. $firstNumbers[2] ) {
+ $firstXNumbers{ $i }++ ;
+}
+for my $i ( $firstNumbers[1] .. $firstNumbers[3] ) {
+ $firstYNumbers{ $i }++ ;
+}
+for my $i ( $secondNumbers[0] .. $secondNumbers[2] ) {
+ $secondXNumbers{ $i }++ ;
+}
+for my $i ( $secondNumbers[1] .. $secondNumbers[3] ) {
+ $secondYNumbers{ $i }++ ;
+}
+my @commonX = grep { exists( $secondXNumbers{ $_ } ) } keys %firstXNumbers ;
+my @commonY = grep { exists( $secondYNumbers{ $_ } ) } keys %firstYNumbers ;
+my $firstArea = ( $firstNumbers[2] - $firstNumbers[0] ) * ( $firstNumbers[3] -
+ $firstNumbers[1] ) ;
+my $secondArea = ( $secondNumbers[2] - $secondNumbers[0] ) * ( $secondNumbers[3] -
+ $secondNumbers[1] ) ;
+if ( @commonX && @commonY ) {
+ my $superPositionArea = ( max( @commonX) - min( @commonX ) ) * ( max( @commonY )
+ - min( @commonY ) ) ;
+ say ( $firstArea + $secondArea - $superPositionArea ) ;
+}
+else {
+ say ( $firstArea + $secondArea ) ;
+}
diff --git a/challenge-152/ulrich-rieke/raku/ch-1.raku b/challenge-152/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..a2a4a3a6f3
--- /dev/null
+++ b/challenge-152/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,27 @@
+use v6 ;
+
+say "Enter a triangle from top to bottom, separating numbers by spaces!" ;
+say "In every line there should be one number more than in the preceding one!" ;
+say "To end entry , enter letter e!" ;
+my $lastLen = 0 ;
+my $len ;
+my $pathSum = 0 ;
+my $line = $*IN.get ;
+while ( $line ne "e" ) {
+ while ( $line !~~ /^(\d \s*)+$/ ) {
+ say "Enter only numbers and spaces!" ;
+ $line = $*IN.get ;
+ }
+ $len = $line.split( /\s+/ ).elems ;
+ unless ( $line eq "e" ) {
+ while ( $len != $lastLen + 1 ) {
+ say "There should be one more number than the last time!" ;
+ $line = $*IN.get ;
+ $len = $line.split( /\s+/ ).elems ;
+ }
+ }
+ $lastLen = $len ;
+ $pathSum += $line.split( /\s+/ ).map( {.Int} ).min ;
+ $line = $*IN.get ;
+}
+say $pathSum ;
diff --git a/challenge-152/ulrich-rieke/raku/ch-2.raku b/challenge-152/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..adf84546bf
--- /dev/null
+++ b/challenge-152/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,36 @@
+use v6 ;
+
+say "Enter the lower left and upper right corner of first rectangle!" ;
+say "Separate numbers by spaces!" ;
+say "Rectangle 1 => " ;
+my $line = $*IN.get ;
+my @first_numbers = $line.split( /\s/ ).map( {.Int} ) ;
+say "Enter the lower left and upper right corner of second rectangle!" ;
+say "Separate numbers by spaces!" ;
+say "Rectangle 2 => " ;
+$line = $*IN.get ;
+my @second_numbers = $line.split( /\s/ ).map( {.Int} ) ;
+#the area covered by the two rectangles is the sum of the areas - the common
+#area if there is one. To compute that we form a set of all x and y values
+#of the 2 respective rectangles. if there is an intersection of the x and y
+#values the common area is the product of ( biggest common x - smallest common x)
+#* ( biggest common y - smallest common y)
+my $firstRectXSet = (@first_numbers[0] .. @first_numbers[2]).Set ;
+my $firstRectYSet = (@first_numbers[1] .. @first_numbers[3]).Set ;
+my $secondRectXSet = (@second_numbers[0] .. @second_numbers[2]).Set ;
+my $secondRectYSet = (@second_numbers[1] .. @second_numbers[3]).Set ;
+my $commonX = $firstRectXSet (&) $secondRectXSet ;
+my $firstArea = (@first_numbers[2] - @first_numbers[0]) *
+ (@first_numbers[3] - @first_numbers[1] ) ;
+my $secondArea = (@second_numbers[2] - @second_numbers[0]) *
+ (@second_numbers[3] - @second_numbers[1] ) ;
+if ( $commonX ) {
+ my $commonY = $firstRectYSet (&) $secondRectYSet ;
+ if ( $commonY ) {
+ say ( $firstArea + $secondArea - (( $commonX.max.key - $commonX.min.key )
+ * ( $commonY.max.key - $commonY.min.key) )) ;
+ }
+}
+else {
+ say ( $firstArea + $secondArea ) ;
+}