4
\$\begingroup\$

You are given the positions of the minute and hour hands of an analog clock as an angle of clockwise rotation from the top (12). So a hand pointing at 9 is at an angle if 270, and a hand pointing exactly in between 12 and 1 is at an angle of 15.

Input will be two integer angles (in any order of your choice) between 0 and 359. Minute hand will be a multiple of 60, hour hand will be a multiple of 5. Output will be the time, given as two integers separated by a colon (and nothing more).

Sample data

(Hour hand first)

195 180 - 6:30
355 300 - 11:50
280 120 - 9:20
0 0 - 12:00
10 120 - 12:20
\$\endgroup\$
  • 1
    \$\begingroup\$ If I understand correctly, the second test cast gives 11:50, not 11:55. Also (hint): you could calculate the time from the hour hand only(!) \$\endgroup\$ – agtoever Mar 5 '16 at 7:33
  • \$\begingroup\$ Yea... since the minute hand is constrained to 60 degree intervals, there shouldn't be any minutes that aren't a multiple of 10. \$\endgroup\$ – Geobits Mar 5 '16 at 8:13
  • \$\begingroup\$ 9:05 is not even valid for this challenge, neither is 280 30. \$\endgroup\$ – ghosts_in_the_code Mar 5 '16 at 10:00
  • 1
    \$\begingroup\$ Surely the hour hand will be a multiple of 5 degrees? \$\endgroup\$ – Neil Mar 5 '16 at 10:34
  • \$\begingroup\$ What's the correct answer for an input of 0 0? \$\endgroup\$ – Neil Mar 5 '16 at 10:35

19 Answers 19

5
\$\begingroup\$

JavaScript ES6, 22 bytes

a=>b=>(0|a/30)+":"+b/6

Try it online (all browsers work)

\$\endgroup\$
5
\$\begingroup\$

Pyth, 9 bytes

j\:.DyQ60

Explanation:

          - autoassign Q = eval_input()
     yQ   -   Q*2
   .D  60 -  divmod(^, 60)
j\:       - ":".join(^)

Try it here

Or try a test suite

\$\endgroup\$
  • \$\begingroup\$ Great approach! \$\endgroup\$ – Luis Mendo Mar 5 '16 at 11:33
3
\$\begingroup\$

Jelly, 13 7 bytes

Le code:

Ḥd60j“:

L'explanation:

         # Uses only the hour hand, (355)
Ḥ        # Double this, (355 × 2 = 710)
 d60     # Divmod 60, ([710 : 60, 710 % 60] = [11, 50])
    j“:  # Join by “:”, (11:50) 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Retina, 93 107 bytes

Byte count assumes ISO 8859-1 encoding

Had to fix a bug, code wasn't working for 0 in input and 0 minutes was shown as a single zero, not two. Fix for 0 in input didn't add any extra characters, but the minutes and new test case added some extra weight...

So...uh...integer division with regex?

(\d+) (\d+)
30$*' $1$*';¶6$*' $2$*';
m+`^('+) \1('*);('*)$
$1 $2;$3'
.+;

('*)¶('*)
$.1:$.2
:0$
:00
\`^0
12

Input is hours and minutes separated by space. Hours come first.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 50 bytes

function t($h,$m){echo floor($h/30)?:12.":".$m/6;}

Set $h and $m to whatever values you like, and off you go.

\$\endgroup\$
  • 2
    \$\begingroup\$ Where do you get $h and $m from? \$\endgroup\$ – andlrc Mar 5 '16 at 9:07
  • \$\begingroup\$ Setting them immediately before the echo. The thread starter never specified how input was handled. \$\endgroup\$ – ricdesi Mar 5 '16 at 18:20
  • \$\begingroup\$ By default all submissions have to be either full programs that read from STDIN and print to STDOUT or functions that read arguments and return values. Assuming that the variables exist makes this a snippet, which we don't allow. \$\endgroup\$ – Alex A. Mar 5 '16 at 23:23
  • \$\begingroup\$ Edited to add function call \$\endgroup\$ – ricdesi Mar 5 '16 at 23:25
1
\$\begingroup\$

Pyth, 11 bytes

++/E30\:/E6

Try it online or Test Suite

Alternative version but the same size:

j\:[/E30/E6

Explanation

Abuses the fact that Python does integer division by default

  /E30       Divide `\` input `E` by 30
 +    \:     Add literal `:`
+       /E6  Add input `E` divided by 6
\$\endgroup\$
0
\$\begingroup\$

Perl 21 + 1 = 22 bytes

$_=($==$_/30).$".<>/6

Requires the -p flag and takes input on multiply lines. Requires -l for multiply inputs:

$ (echo $'195\n180';echo $'355\n300';echo $'280\n120') | perl -pl analclock.pl
6 30
11 50
9 20

Assigning to $= will always yield an integer print($= = 1.23) => 1

\$\endgroup\$
0
\$\begingroup\$

R, 29 bytes

cat(scan()%/%c(30,6),sep=':')
\$\endgroup\$
0
\$\begingroup\$

CJam, 11 bytes

ri30/':ri6/

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 11 bytes

Code:

30/ï':I6/ïJ

Explanation:

30/          # Divide implicit input by 30
   ï         # Convert to int
    ':       # Add the ':'-character
      I6/ï   # Input divided by 6 and converted to int
          J  # Join the stack

Try it online

Uses CP-1252 encoding.

\$\endgroup\$
0
\$\begingroup\$

Pyke, 10 9 bytes

2*60.DRJ:

Explanation:

2*        - a = eval_input_or_not() * 2
  60.D    - a,b = divmod(a, 60)
      R   - a,b = rot_2(a,b)
       J: - ":".join([a,b])
\$\endgroup\$
0
\$\begingroup\$

MATL, 10 bytes

720/2$15XO

The hour in the output has a leading zero if less than 10.

Try it online!

720/      % take first input and divide by 720
2$15XO    % format as HH:MM
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 54 bytes

{sprintf "%d:%02d",($^a div 30||12),$^b/6}

This accounts for 0 hours -> 12:00, as well as having two digits in the minutes place

\$\endgroup\$
0
\$\begingroup\$

DUP, 12 bytes

[2*60/.':,.]

Try it here.

Anonymous lambda that takes minutes then hours as arguments. Usage:

180 195[2*60/.':,.]!

Explanation

[          ] {lambda}
 2*          {double hours}
   60/       {divmod by 60}
      .      {output quotient}
       ':,   {output :}
          .  {output remainder}
\$\endgroup\$
0
\$\begingroup\$

Milky Way 1.6.5, 27 bytes

'2*60m":";+;?{_;+_^12;+0+}!

Explanation

'2*                          ` double the input (195) -> [390]
   60m                       ` divmod TOS by 60 -> [6, 30]
      ":";+;                 ` add a colon -> [":30", 6]
            ?{_;+_^12;+0+}   ` format HH:MM -> ["6:30"]
                          !  ` output TOS
\$\endgroup\$
0
\$\begingroup\$

Japt, 14 bytes

U/30|0 +':+V/6

Test it online!

I really need to implement a shorter way to floor...

\$\endgroup\$
0
\$\begingroup\$

Python 2.7, 35 28 characters

Needs hour hand as a first input; ignores the minute hand input.

lambda i:`i/30`+":"+`i*2%60`

7 characters less thanks to @DenkerAffe by using lambda instead of input() and print.

\$\endgroup\$
  • 1
    \$\begingroup\$ 28 bytes if you go with an anonymous lambda which is always allowed unless explicitly forbidden. lambda i:i/30+":"+i*2%60`` \$\endgroup\$ – Denker Mar 5 '16 at 11:02
0
\$\begingroup\$

CJam, 11 bytes

ri2*60md':\

An alternate approach using divmod.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Lua, 101 Bytes

A solution only using the hour argument. It is a full program taking the input via command-line argument lua readAnalogClock.lua 355. You could call it with 2 arguments, but the second one is never used.

i=arg[1]j=i%30i=(i-j)/30print(((i<1 and 12or i)..''):gsub("%..*","")..":"..(2*j..''):gsub("%..*",""))

Ungolfed

i=arg[1]                  -- initialise i with the hours angle
j=i%30                    -- initialise j with the minute part of i
i=(i-j)/30                -- round i to the previous hour
print(                    
  ((i<1 and 12or i)..'')  -- replace 0 by 12 and make it a string
    :gsub("%..*","")      -- remove decimal part of hour (which is .0)
  ..":"                   -- concatenate with the separator
  ..(2*j..'')             -- makes a string containing the minutes
    :gsub("%..*",""))     -- remove decimal part of minutes
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.