Camel
Peter
Peter Campbell Smith

Bits and angles

Weekly challenge 120 — 5 July 2021

Week 120: 5 Jul 2021

Task 2

Task — Clock angle

You are given time $T in the format hh:mm. Write a script to find the smaller angle formed by the hands of an analog clock at the given time.

Examples


Example 1
Input: $T = '03:10'
Output: 35 degree
The distance between the 2 and the 3 on the clock is 30 
   degree.
For the 10 minutes i.e. 1/6 of an hour that have passed.
The hour hand has also moved 1/6 of the distance between 
   the 3 and the 4, which adds 5 degree (1/6 of 30).
The total measure of the angle is 35 degree.
Input: $T = '04:00'
Output: 120 degree

Analysis

An interesting challenge! Let's split the input into $hrs and $mins.

The angle in degrees between clock 12 and the current hour hand position is $hrs * 30 + $mins * 0.5, because the hour hand moves 360 degrees in 12 hours, ie 30 degrees per completed hour, and in addition it moves 0.5 degrees per minute beween the hours because it takes 12 x 60 = 720 minutes for the hour hand to make a full 12 hour revolution, and that's 0.5 degrees per minute.

The angle between clock 12 and the minute hand is $mins * 6. This is becasue the minute hand makes a 360 degree revolution in an hour so 360 / 60 degrees each minute.

The angle required by the challenge is the difference between these two angles. It could be positive or negative but we're only interested in the absolute value, so let's abs it, and if the angle is more than 180 degrees we want the other side of the hands, which will be 360 - $angle degrees apart.

The only slight tweak needed is that in the initial value of $hrs we need $hrs % 12, because 12 o'clock is really zero o'clock, and by taking the modulus we conveniently also cater for 24-hour clock times like 18:30 or 23:59.

Try it 

Try running the script with any input:



example: 12:02

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2021-07-05
use utf8;     # Week 120 - task 2 - Clock angle
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

clock_angle('3:10');
clock_angle('4:00');
clock_angle('9:00');
clock_angle('17:50');
clock_angle('14:50');
clock_angle('9:17');
clock_angle('23:59');

sub clock_angle {
    
    my ($hrs, $mins, $hrs_angle, $mins_angle, $angle);
    
    # initialise
    ($hrs, $mins) = $_[0] =~ m|(\d+):(\d\d)|;
    
    # get angles from 12:00
    $hrs_angle = ($hrs % 12) * 30 + $mins * 0.5;
    $mins_angle = $mins * 6;
    
    # normalise answer
    $angle = abs($hrs_angle - $mins_angle);
    $angle = 360 - $angle if $angle > 180;
    
    say qq[\nInput:  $hrs:$mins];
    say qq[Output: $angle°];
}

last updated 2026-03-13 — 9 lines of code

Output


Input:  3:10
Output: 35°

Input:  4:00
Output: 120°

Input:  9:00
Output: 90°

Input:  17:50
Output: 125°

Input:  14:50
Output: 145°

Input:  9:17
Output: 176.5°

Input:  23:59
Output: 5.5°

 

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