perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 348 - Task 2: Covert Time

  1 #!/usr/bin/env perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-348/#TASK2
  3 #
  4 # Task 2: Covert Time
  5 # ===================
  6 #
  7 # You are given two strings, $source and $target, containing time in 24-hour time form.
  8 #
  9 # Write a script to convert the source into target by performing one of the following operations:
 10 #
 11 # 1. Add  1 minute
 12 # 2. Add  5 minutes
 13 # 3. Add 15 minutes
 14 # 4. Add 60 minutes
 15 #
 16 # Find the total operations needed to get to the target.
 17 #
 18 # Example 1
 19 #
 20 # Input: $source = "02:30"
 21 #        $target = "02:45"
 22 # Output: 1
 23 #
 24 # Just one operation i.e. "Add 15 minutes".
 25 #
 26 #
 27 # Example 2
 28 #
 29 # Input: $source = "11:55"
 30 #        $target = "12:15"
 31 # Output: 2
 32 #
 33 # Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes".
 34 #
 35 #
 36 # Example 3
 37 #
 38 # Input: $source = "09:00"
 39 #        $target = "13:00"
 40 # Output: 4
 41 #
 42 # Four operations of "Add 60 minutes".
 43 #
 44 #
 45 # Example 4
 46 #
 47 # Input: $source = "23:45"
 48 #        $target = "00:30"
 49 # Output: 3
 50 #
 51 # Three operations of "Add 15 minutes".
 52 #
 53 #
 54 # Example 5
 55 #
 56 # Input: $source = "14:20"
 57 #        $target = "15:25"
 58 # Output: 2
 59 #
 60 # Two operations, one "Add 60 minutes" and one "Add 5 minutes"
 61 #
 62 ############################################################
 63 ##
 64 ## discussion
 65 ##
 66 ############################################################
 67 #
 68 # We turn the hours:minutes into minutes since the start of the day.
 69 # Then we calculate the diff: If the source is bigger than the target,
 70 # the diff is the minutes until end of day plut the minutes of target,
 71 # in the other case it's just the diff from source to target.
 72 # Then we check how many times we can fill 60 minutes into this diff,
 73 # then how many additional 15 minutes, then how many additional 5 minutes,
 74 # then how many single minutes.
 75 
 76 use v5.36;
 77 
 78 my $minutes_per_day = 24 * 60;
 79 
 80 convert_time("02:30", "02:45");
 81 convert_time("11:55", "12:15");
 82 convert_time("09:00", "13:00");
 83 convert_time("23:45", "00:30");
 84 convert_time("14:20", "15:25");
 85 
 86 sub convert_time($source, $target) {
 87     say "Input: \$source = '$source', \$target = '$target'";
 88     my ($h, $m) = split /:/, $source;
 89     $h =~ s/^0//;
 90     $m =~ s/^0//;
 91     my $minutes_source = $h * 60 + $m;
 92     ($h, $m) = split /:/, $target;
 93     $h =~ s/^0//;
 94     $m =~ s/^0//;
 95     my $minutes_target = $h * 60 + $m;
 96     if($minutes_source == $minutes_target) {
 97         return say "Output: 0";
 98     }
 99     my $diff = $minutes_target - $minutes_source;
100     if($minutes_source > $minutes_target) {
101         $diff = $minutes_target + $minutes_per_day - $minutes_source;
102     }
103     my $count = 0;
104     while($diff >= 60) {
105         $diff -= 60;
106         $count++;
107     }
108     while($diff >= 15) {
109         $diff -= 15;
110         $count++;
111     }
112     while($diff >= 5) {
113         $diff -= 5;
114         $count++;
115     }
116     $count += $diff;
117     say "Output: $count";
118 }