## Advent Calendar - December 10, 2021

Friday, Dec 10, 2021| Tags: Raku

### | Day 9 | Day 10 | Day 11 |

The gift is presented by Roger Bell_West. Today he is talking about his solution to “The Weekly Challenge - 120”. This is re-produced for Advent Calendar 2021 from the original post by `Roger Bell_West`.

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 a given time.

Which seems again as though it has a fairly straightforward solution: determine the angle of each hand, then reduce the difference.

### Raku:

``````sub ca(\$n) {
my \$a=0;
``````

Extract hour and minute:

``````  if (\$n ~~ /(<[0..9]>+)\:(<[0..9]>+)/) {
``````

Convert each one to an angle (note that each minute puts half a degree on the hour hand, so we’re in floating point territory):

``````    my (\$ha,\$ma)=map {\$_ % 360}, (\$0*30+\$1/2,\$1*6);
``````

Take the absolute difference:

``````    \$a=abs(\$ha-\$ma);
``````

Reduce until we get an angle lying in (-180..180):

``````    while (\$a > 180) {
\$a-=360;
}
``````

And take the absolute again.

``````    \$a=abs(\$a);
}
return \$a;
}
``````

The complete solution in Raku.

``````#! /usr/bin/perl6

use Test;

plan 2;

is(ca('03:10'),35,'example 1');
is(ca('04:00'),120,'example 2');

sub ca(\$n) {
my \$a=0;
if (\$n ~~ /(<[0..9]>+)\:(<[0..9]>+)/) {
my (\$ha,\$ma)=map {\$_ % 360}, (\$0*30+\$1/2,\$1*6);
\$a=abs(\$ha-\$ma);
while (\$a > 180) {
\$a-=360;
}
\$a=abs(\$a);
}
return \$a;
}
``````

If you have any suggestion then please do share with us perlweeklychallenge@yahoo.com.