Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
1  
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(!) – agtoever Mar 5 at 7:33
    
Yea... since the minute hand is constrained to 60 degree intervals, there shouldn't be any minutes that aren't a multiple of 10. – Geobits Mar 5 at 8:13
    
9:05 is not even valid for this challenge, neither is 280 30. – ghosts_in_the_code Mar 5 at 10:00
1  
Surely the hour hand will be a multiple of 5 degrees? – Neil Mar 5 at 10:34
    
What's the correct answer for an input of 0 0? – Neil Mar 5 at 10:35

19 Answers 19

JavaScript ES6, 22 bytes

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

Try it online (all browsers work)

share|improve this answer

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

share|improve this answer
    
Great approach! – Luis Mendo Mar 5 at 11:33

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!

share|improve this answer

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!

share|improve this answer

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.

share|improve this answer
2  
Where do you get $h and $m from? – andlrc Mar 5 at 9:07
    
Setting them immediately before the echo. The thread starter never specified how input was handled. – ricdesi Mar 5 at 18:20
    
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. – Alex A. Mar 5 at 23:23
    
Edited to add function call – ricdesi Mar 5 at 23:25

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
share|improve this answer

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

share|improve this answer

R, 29 bytes

cat(scan()%/%c(30,6),sep=':')
share|improve this answer

CJam, 11 bytes

ri30/':ri6/

Try it online!

share|improve this answer

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.

share|improve this answer

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])
share|improve this answer

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
share|improve this answer

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

share|improve this answer

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}
share|improve this answer

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
share|improve this answer

Japt, 14 bytes

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

Test it online!

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

share|improve this answer

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.

share|improve this answer
1  
28 bytes if you go with an anonymous lambda which is always allowed unless explicitly forbidden. lambda i:i/30+":"+i*2%60`` – DenkerAffe Mar 5 at 11:02

CJam, 11 bytes

ri2*60md':\

An alternate approach using divmod.

Try it online!

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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