Peter’s blog ✴ Week 366 ✴ 23 March 2026

THE WEEKLY CHALLENGE
Prefixes and times?

The Perl Camel

Task 2

Valid times

You are given a time in the form ‘HH:MM’. The earliest possible time is ‘00:00’ and the latest possible time is ‘23:59’. In the string time, the digits represented by the ‘?’ symbol are unknown, and must be replaced with a digit from 0 to 9.

Write a script to return the count different ways we can make it a valid time.

Examples


Example 1
Input: $time = '?2:34'
Output: 3
0 -> '02:34' valid
1 -> '12:34' valid
2 -> '22:34' valid

Example 2
Input: $time = '?4:?0'
Output: 12
Hours: tens digit ?, ones digit 4 -> can be 04,
   and 14 (2 possibilities)
Minutes: tens digit ?,
   ones digit 0 -> tens can be 0-5 (6 possibilities)
Total: 2 × 6 = 12

Example 3
Input: $time = '??:??'
Output: 1440
Hours: from 00 to 23 -> 24 possibilities
Minutes: from 00 to 59 -> 60 possibilities
Total: 24 × 60 = 1440

Example 4
Input: $time = '?3:45'
Output: 3
If tens digit is 0 or 1 -> any ones digit works,
   so 03 and 13 are valid
If tens digit is 2 -> ones digit must be 0-3,
   but here ones digit is 3, so 23 is valid
Therefore: 0,1,2 are all valid -> 3 possibilities

Example 5
Input: $time = '2?:15'
Output: 4
Tens digit is 2, so hours can be 20-23
Ones digit can be 0,1,2,3 (4 possibilities)
Therefore: 4 valid times

Analysis

This is a great challenge in that itis simple to explain but quite tricky to solve.

The minutes are fairly easy: if the first character is '?' then there are 6 possibilities (0-5), and if the second is '?' there are 10 (0-9). These are independent, so overall there are 60 possibilities for '??';

The hours are trickier because there are 5 patteerns to consider:

  • '?' followed by 4-9 has 2 possible values (0 or 1)
  • '?' followed by 0-3 has 3 possibles (0, 1 or 2)
  • '2' followed by '?' yields 4 possibilities (0, 1, 2 or 3)
  • '0' or '1' followed by '?' can be any digit, ie 10 possibilities
  • '??' has 24 possibles (00-23)

If there are multiple '?'s the numbers of possibilities have to be multiplied together.

And for a problem like this it's always sensible to check the premise that the input is valid, so I did that.

Perl Weekly’s review

from PW issue 766

Peter Campbell Smith's Week 366 Write-up provides an unambiguous, pragmatic solution style representing a strong real-world Perl mindset. The emphasis is placed on solving the problem in an accurate and efficient manner through simple implementation methods. The provided solution is straightforward and effective; he understands the relevant tasks thoroughly and prefers to solve issues clearly and without complexity (typical of all Weekly Challenges).

This review may cover either or both challenges for this week.

Try it 

Try running the script with any input:



example: 0?:09

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-03-23
use utf8;     # Week 366 - task 2 - Valid times
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

valid_times('??:??');
valid_times('12:34');
valid_times('?2:34');
valid_times('1?:34');
valid_times('12:?4');
valid_times('12:3?');
valid_times('?7:00');
valid_times('24:00');

sub valid_times {
    
    my ($time, @d, $output, $hrs, $check);
    
    # initialise
    $time = $_[0];
    say qq[\nInput:  '$time'];
    
    # validate
    $check = $time;
    $check =~ s|\?|0|g;
    if ($check !~ m|^(\d\d):(\d\d)$| or $1 > 23 or $2 > 59) {
        say 'Output: invalid time';
        return;
    }
    
    # calculate result - mins
    @d = $time =~ m|^([012\?])([0-9\?]):([0-5\?])([0-9\?])$|;
    $output = 1;
    $output *= 10 if $d[3] eq '?';
    $output *= 6  if $d[2] eq '?';
    
    # and hrs
    $hrs = $d[0] . $d[1];
    $output *= 2  if $hrs =~ m|\?[4-9]|;
    $output *= 3  if $hrs =~ m|\?[0-3]|;
    $output *= 4  if $hrs =~ m|2\?|;
    $output *= 10 if $hrs =~ m|[01]\?|;
    $output *= 24 if $hrs =~ m|\?\?|;
    
    say qq[Output: $output];
}

20 lines of code

Output from script


Input:  '??:??'
Output: 1440

Input:  '12:34'
Output: 1

Input:  '?2:34'
Output: 3

Input:  '1?:34'
Output: 10

Input:  '12:?4'
Output: 6

Input:  '12:3?'
Output: 10

Input:  '?7:00'
Output: 2

Input:  '24:00'
Output: invalid time

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain