aboutsummaryrefslogtreecommitdiff
path: root/challenge-259
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-07 19:19:54 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-07 19:19:54 +0000
commit510484bb98b0d2413f8c09c0a827e91b992d2533 (patch)
tree432e5f352b763b4e5ff6c177ad8417f93571880f /challenge-259
parent37d52170de0afe09bf2abcba002437f381322c80 (diff)
downloadperlweeklychallenge-club-510484bb98b0d2413f8c09c0a827e91b992d2533.tar.gz
perlweeklychallenge-club-510484bb98b0d2413f8c09c0a827e91b992d2533.tar.bz2
perlweeklychallenge-club-510484bb98b0d2413f8c09c0a827e91b992d2533.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-259')
-rwxr-xr-xchallenge-259/ulrich-rieke/cpp/ch-1.cpp61
-rwxr-xr-xchallenge-259/ulrich-rieke/haskell/ch-1.hs44
-rwxr-xr-xchallenge-259/ulrich-rieke/perl/ch-1.pl46
-rwxr-xr-xchallenge-259/ulrich-rieke/perl/ch-2.pl30
-rwxr-xr-xchallenge-259/ulrich-rieke/raku/ch-1.raku26
-rwxr-xr-xchallenge-259/ulrich-rieke/rust/ch-1.rs49
6 files changed, 256 insertions, 0 deletions
diff --git a/challenge-259/ulrich-rieke/cpp/ch-1.cpp b/challenge-259/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..020c8de1e7
--- /dev/null
+++ b/challenge-259/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,61 @@
+#include <iostream>
+#include <vector>
+#include <chrono>
+#include <string>
+#include <string_view>
+#include <ranges>
+#include <algorithm>
+
+std::chrono::year_month_day stringToDate( const std::string & input ) {
+ std::vector<std::string> dateParts ;
+ char delimiter = '-' ;
+ auto split = input | std::views::split( delimiter ) ;
+ for ( const auto& subRange : split ) {
+ std::string datePart( &*subRange.begin() ,
+ std::ranges::distance( subRange ) ) ;
+ dateParts.push_back( datePart ) ;
+ }
+ std::chrono::year y ( std::stoi( dateParts[0] ) ) ;
+ std::chrono::month m (static_cast<unsigned>( std::stoi( dateParts[1] )))
+ ;
+ std::chrono::day d ( static_cast<unsigned>(std::stoi( dateParts[2] ))) ;
+ std::chrono::year_month_day date ( y , m , d ) ;
+ return date ;
+}
+
+int main( ) {
+ std::cout << "Enter a start date in the form <year>-<month>-<day>!\n" ;
+ std::string start ;
+ std::getline( std::cin , start ) ;
+ std::cout << "Enter a day offset!\n" ;
+ std::string offsetstr ;
+ std::getline( std::cin , offsetstr ) ;
+ int offset { std::stoi( offsetstr ) } ;
+ std::cout << "Enter bank holidays, each day in the format above!\n" ;
+ std::cout << "Enter <return> to end!\n" ;
+ std::vector<std::string> holidays ;
+ std::string free ;
+ std::getline( std::cin , free ) ;
+ while ( free.length( ) > 0 ) {
+ holidays.push_back( free ) ;
+ std::getline( std::cin , free ) ;
+ }
+ std::vector<std::chrono::year_month_day> bank_holidays ;
+ for ( auto s : holidays ) {
+ bank_holidays.push_back( stringToDate( s )) ;
+ }
+ std::chrono::time_point theStart { std::chrono::sys_days {
+ stringToDate( start ) }} ;
+ while ( offset != 0 ) {
+ theStart += std::chrono::days{ 1 } ;
+ std::chrono::weekday cur_weekday{ theStart } ;
+ if ( cur_weekday != std::chrono::Saturday && cur_weekday !=
+ std::chrono::Sunday &&
+ std::find( bank_holidays.begin( ) , bank_holidays.end( ) ,
+ theStart ) == bank_holidays.end( ) ) {
+ offset-- ;
+ }
+ }
+ std::cout << theStart << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-259/ulrich-rieke/haskell/ch-1.hs b/challenge-259/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..011085c255
--- /dev/null
+++ b/challenge-259/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,44 @@
+module Challenge259
+ where
+import Data.Time.Calendar
+import Data.List.Split ( splitOn )
+import Data.List ( (!!) )
+
+solution :: Day -> Int -> [Day] -> Day
+solution start offset holidays = snd $ until ( (== 0 ) . fst ) step
+ (offset , start )
+ where
+ step :: (Int, Day) -> (Int , Day)
+ step (currentnum , currentdate ) = if (dayOfWeek nextDay == Saturday)
+ || (dayOfWeek nextDay == Sunday ) || elem nextDay holidays then
+ (currentnum , nextDay) else ( currentnum - 1 , nextDay )
+ where
+ nextDay :: Day
+ nextDay = addDays 1 currentdate
+
+getHolidays :: IO [String]
+getHolidays = do
+ dayOff <- getLine
+ if ( not $ null dayOff ) then do
+ restOfDays <- getHolidays
+ return (dayOff:restOfDays)
+ else do
+ return []
+
+stringToDay :: String -> Day
+stringToDay str = fromGregorian (toInteger $ head dateparts ) ( dateparts !!
+ 1 ) ( dateparts !! 2 )
+ where
+ dateparts = map read $ splitOn "-" str
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a start date in the form <year>-<month>-<day>!"
+ startDate <- getLine
+ putStrLn "Enter a day offset!"
+ offset <- getLine
+ putStrLn "Enter some bank holidays, each date in the format above!"
+ putStrLn "Enter <return> to end entry!"
+ holidays <- getHolidays
+ print $ showGregorian $ solution (stringToDay startDate ) (read offset)
+ (map stringToDay holidays )
diff --git a/challenge-259/ulrich-rieke/perl/ch-1.pl b/challenge-259/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..8cecfcc411
--- /dev/null
+++ b/challenge-259/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use DateTime ;
+use List::Util qw ( none ) ;
+
+say "Enter a start date!" ;
+my $start = <STDIN> ;
+chomp $start ;
+while ( $start !~ /(\d{4})-(\d{1,2})\-(\d{1,2})/ ) {
+ say "incorrect date format! Re-enter!" ;
+ $start = <STDIN> ;
+ chomp $start ;
+}
+(my $year , my $month , my $day) = split( /-/ , $start ) ;
+say "Enter a day offset!" ;
+my $offset = <STDIN> ;
+chomp $offset ;
+while ( $offset !~ /^\d+$/ ) {
+ say "Only digits are allowed! Re-enter!" ;
+ $offset = <STDIN> ;
+ chomp $offset ;
+}
+say "Are there any bank holidays to consider? If so, enter in date format each!";
+say "Enter <return> to end!" ;
+my @holidays ;
+my $free = <STDIN> ;
+chomp $free ;
+while ( $free ) {
+ push @holidays, $free ;
+ $free = <STDIN> ;
+ chomp $free ;
+}
+my $dtcurrent = DateTime->new(
+ year => $year ,
+ month => $month ,
+ day => $day ,
+ ) ;
+while ($offset != 0 ) {
+ $dtcurrent->add( days => 1 ) ;
+ if ( $dtcurrent->day_of_week < 6 && (none { $dtcurrent->ymd eq $_ } @holidays) ) {
+ $offset-- ;
+ }
+}
+say $dtcurrent->ymd ;
diff --git a/challenge-259/ulrich-rieke/perl/ch-2.pl b/challenge-259/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..fd4b818c91
--- /dev/null
+++ b/challenge-259/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a string according to given limitations!" ;
+my $string = <STDIN> ;
+chomp $string ;
+#separate checking of %} at the end since both { and } in the same
+#regex do weird things
+if ( $string =~ /%}$/ && $string =~
+ /^{%\s+(\w+?)\s+(((\w+)=("*[a-z"\\A-Z\d\s]+"*)\s*)*)\s+/ ) {
+ say "{" ;
+ say " name => $1," ;
+ say " fields => {" ;
+ if ( $2 ) {
+ my $text = $2 ;
+ while ( $text =~ /\G\s*(\w+)=(".+"\s*|\d+\s*)/g ) {
+ print " $1 => " ;
+ if ( $2 =~ /^\d+$/ ) {
+ say $2 . ",";
+ }
+ else {
+ say "$2," ;
+ }
+ }
+ }
+ say " }" ;
+ say "}" ;
+}
diff --git a/challenge-259/ulrich-rieke/raku/ch-1.raku b/challenge-259/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..e11f111491
--- /dev/null
+++ b/challenge-259/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,26 @@
+use v6 ;
+
+say "Enter a date in the format year-month-day!" ;
+my $date = $*IN.get ;
+while ( $date !~~ /\d **4 \- \d ** 2 \- \d ** 2/ ) {
+ say "Enter in the correct format!" ;
+ $date = $*IN.get ;
+}
+say "Enter a day offset!" ;
+my $offset = $*IN.get ;
+say "Enter any bank holidays in the above format, <return> to end!" ;
+my @holidays ;
+my $free = $*IN.get ;
+while ( $free ) {
+ @holidays.push( $free ) ;
+ $free = $*IN.get ;
+}
+(my $year , my $month , my $day ) = $date.split( /\-/ ) ;
+my $currentDate = Date.new( $year.Int , $month.Int , $day.Int ) ;
+while ( $offset != 0 ) {
+ $currentDate = $currentDate + 1;
+ if ( $currentDate.day-of-week < 6 && ( so ~$currentDate eq @holidays.none ) ) {
+ $offset-- ;
+ }
+}
+say $currentDate ;
diff --git a/challenge-259/ulrich-rieke/rust/ch-1.rs b/challenge-259/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..1586c218f9
--- /dev/null
+++ b/challenge-259/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,49 @@
+use std::io ;
+use chrono::{NaiveDate , Datelike , Weekday} ;
+use std::io::BufRead ;
+
+fn main() -> io::Result<()> {
+ println!("Enter a starting day in form year-month-day!");
+ let mut dateline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut dateline ).unwrap( ) ;
+ let dateparts : Vec<u32> = dateline.split("-").map( | p | p.trim( ).
+ parse::<u32>( ).unwrap( )).collect( ) ;
+ let current : Option<NaiveDate> = NaiveDate::from_ymd_opt( dateparts[0] as i32 ,
+ dateparts[1] , dateparts[2] ) ;
+ println!("Enter a day offset!") ;
+ let mut os : String = String::new( ) ;
+ io::stdin( ).read_line( &mut os ).unwrap( ) ;
+ let someline : &str = &*os ;
+ let mut offset : u32 = someline.trim( ).parse::<u32>( ).unwrap( ) ;
+ println!("Enter some bank holidays, in the same way as above!") ;
+ println!("Enter <return> to end!") ;
+ let mut lines = io::stdin( ).lock( ).lines( ) ;
+ let mut all_input : String = String::new( ) ;
+ while let Some( line ) = lines.next( ) {
+ let last_input = line.unwrap( ) ;
+ if last_input.len( ) == 0 {
+ break ;
+ }
+ else {
+ all_input.push_str( &last_input ) ;
+ all_input.push_str( "\n" ) ;
+ }
+ }
+ let holidays : Vec<&str> = all_input.split( "\n" ).collect( ) ;
+ let mut current_day : NaiveDate = current.unwrap( ) ;
+ let mut nddi = current_day.iter_days( ) ;
+ offset = offset + 1 ;
+ while offset != 0 {
+ let next_day = nddi.next( ).unwrap( ) ;
+ let nd_string = next_day.format("%Y-%m-%d").to_string( ) ;
+ let nd_str = nd_string.as_str( ) ;
+ let wd = next_day.weekday( ) ;
+ if wd != Weekday::Sat && wd != Weekday::Sun && ! holidays.contains(
+ &nd_str ) {
+ offset = offset - 1 ;
+ }
+ current_day = next_day.clone( ) ;
+ }
+ println!("{}" , current_day.format("%Y-%m-%d").to_string( ) ) ;
+ Ok(())
+}