Data representation

 Slow periods:

   07:00 - 10:00
   15:00 - 19:00

 Input: (sample):

    05:00 


It's easier to convert the HH:MM time format to #minutes: Slow periods: 420 - 600 (07:00 - 10:00) 900 - 1140 (15:00 - 19:00) Input: (sample): 300 (convert 05:00 !)

Problem analysis

 This is a Math problem.

 Speed graph over time:

     

 

Problem analysis

  The distance travelled per min outside congestion = 1

  In T min, distance travelled = T

     

 

Problem analysis

  The distance travelled per min inside congestion = 0.5

  In T min, distance travelled = 0.5×T

     

 

Problem analysis

  The question ask to find Tend where:

  Area from Tstart to Tend = 120 min (= 2 hrs)

     

  I.e.: TstartTend T(t) dt = 120
 

Problem analysis

 You can technically solve this problem analytically

 But this is Computer Science...

 So we do it algorithmically  


Discrete Event Simulation: A computing technique that simulate the passage of time and perform calculations to mimic what happens in the real world
How to apply Discrete Event Simulation to J4: double totCommTime = 2*60; // 2 hrs while ( totCommTime > 0 ) { Simulate what happens for 1 min }

Solution    in pseudo code

   double totCommTime = 2*60;   // 2 hrs

   int startHr, startMin;
   scanf("%d:%d", &startHr, &startMin);

   int now = startHr*60 + startMin;  // Current time in #minutes

   while ( totCommTime > 0.0 )
   {
      Simulate commute travel for 1 min




      now++;       
   }

  (Print now in HH:MM format for answer)

Solution    in (almost) C++

   double totCommTime = 2*60;   // 2 hrs

   int startHr, startMin;
   scanf("%d:%d", &startHr, &startMin);

   int now = startHr*60 + startMin;  // Current time in #minutes

   while ( totCommTime > 0.0 )
   {
      if ( now is OUTSIDE the slow periods )
         totCommTime -= 1.0; // Each passing min cuts commute time by 1 min
      else
         totCommTime -= 0.5; // We make 1/2 the progress in slow periods...

      now++;       
   }

  (Print now in HH:MM format for answer)  

DEMO: CCC/2016/Progs/j4.cc

Better solution    integer solution (in case there is round off errors)

   int totCommTime = 2*2*60;   // We use 0.5 min as time unit !

   int startHr, startMin;
   scanf("%d:%d", &startHr, &startMin);

   int now = startHr*60 + startMin;  // Current time in #minutes

   while ( totCommTime > 0 )
   {
      if ( now is OUTSIDE the slow periods )
         totCommTime -= 2; // 2 half min = 1 min
      else
         totCommTime -= 1; // 1 half min...

      now++;       
   }

  (Print now in HH:MM format for answer)  

DEMO: CCC/2016/Progs/j4-int.cc