String time
Weekly challenge 348 — 17 November 2025
Week 348: 17 Nov 2025
You are given two strings, $source and $target, containing time in 24-hour time form.
Write a script to convert $source into $target by performing the following operations:
Find the number of operations needed to get to the target.
Example 1 Input: $source = '02:30' $target = '02:45' Output: 1 Just one operation i.e. 'Add 15 minutes'. Example 2 Input: $source = '11:55' $target = '12:15' Output: 2 Two operations i.e. 'Add 15 minutes' followed by 'Add 5 minutes'. Example 3 Input: $source = '09:00' $target = '13:00' Output: 4 Four operations of 'Add 60 minutes'. Example 4 Input: $source = '23:45' $target = '00:30' Output: 3 Three operations of 'Add 15 minutes'. Example 5 Input: $source = '14:20' $target = '15:25' Output: 2 Two operations, one 'Add 60 minutes' and one 'Add 5 minutes'
My solution starts by converting the two times to minutes since midnight, adding 24 hours to the target time if it's earlier than the source time, and then subtracting source from target to get the gap between them in minutes.
I then note that all the operation units are multiples of the next smaller one. That means that, starting with the largest, I can just subtract units from the gap until the gap is smaller than the unit, and then proceed to the next smaller unit.
The maximum number of operations is 32, required for 23 hours and 59 minutes. We aren't told what to do if source and target are the same, but that's either 0 minutes, requiring 0 operations or 24 hours, requiring 24 operations.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2025-11-17 use utf8; # Week 348 - task 2 - Convert time use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; convert_time('02:30', '02:45'); convert_time('11:55', '12:15'); convert_time('09:00', '13:00'); convert_time('23:45', '00:30'); convert_time('14:20', '15:25'); convert_time('00:00', '23:59'); sub convert_time { my ($from, $till, @chunks, $from_mins, $till_mins, $gap, $j, $ops, $explain); # initialise ($from, $till) = @_; @chunks = (60, 15, 5, 1); $explain = ''; # convert to minutes since midnight $from_mins = substr($from, 0, 2) * 60 + substr($from, 3, 2); $till_mins = substr($till, 0, 2) * 60 + substr($till, 3, 2); $till_mins += 24 * 60 if $till_mins < $from_mins; $gap = $till_mins - $from_mins; # analyse difference $ops = 0; for $j (0 .. 3) { while ($gap >= $chunks[$j]) { $gap -= $chunks[$j]; $ops ++; $explain .= qq[$chunks[$j] + ]; } } say qq[\nInput: \$source = '$from', \$target = '$till']; say qq[Output: $ops : ] . substr($explain, 0, -2); }
Input: $source = '02:30', $target = '02:45' Output: 1 : 15 Input: $source = '11:55', $target = '12:15' Output: 2 : 15 + 5 Input: $source = '09:00', $target = '13:00' Output: 4 : 60 + 60 + 60 + 60 Input: $source = '23:45', $target = '00:30' Output: 3 : 15 + 15 + 15 Input: $source = '14:20', $target = '15:25' Output: 2 : 60 + 5 Input: $source = '00:00', $target = '23:59' Output: 32 : 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 60 + 15 + 15 + 15 + 5 + 5 + 1 + 1 + 1 + 1
Any content of this website which has been created by Peter Campbell Smith is in the public domain