Solution - Problem from the book "Programming Challenges" : 1




The Problem:
                Problem Number: 2.8.3
                Title: Hartal
                Info: PC/UVa IDs: 110203/10050
                Popularity: B
                Success rate: high
                Level: 2



Considering a simulation of 22 days, we get a calender like this,


1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su


Now look,
                Fridays are at 6,13,20 ... Saturdays are at 7,14,21 ...
                This means, each date is a Saturday if it is completely divisible by 7 and
                each date is a Friday if (That Date + 1) is completely divisible by 7

This is the only crucial thing here in my program and I think that the rest of the process is easy to understand.

Here is the full source code.

$total_days = 100; /*Total Number of Days to Simulate*/
$party_hartal = "12,15,25,40"; /*Hartal sequence of each party*/
$h = explode(",",$party_hartal);
$hartals = array();

foreach($h as $x){ /*Looping through each hartal sequence*/
 $increment = $x;
 while($x <= $total_days){
  $offday = false;
  
  //Here checking this date is an offday (friday or saturday)
  if( ( ($x + 1) % 7 == 0 ) || ( $x % 7 == 0 ) ) $offday = true;

  //If not offday then keeping it as an hartal day
  if(!$offday) $hartals[$x] = $x;

  $x+=$increment;
  }
 }

echo "Days lost: ".count($hartals);

Click the below given link to get full description of the problem.

Problem from the book "Programming Challenges" : 1





Recommended Recommends

Comments

Contact Us

Loading...