20
\$\begingroup\$

I can have a large if/else condition for each 30 minutes but I'm looking for more math and Unicode based solution.

Here are clock emojis: 🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛🕜🕝🕞🕟🕠🕡🕢🕣🕤🕥🕦🕧. If you lack proper rendering support, you can see them below (they're not in the same order and they may look different to what you see) or at the Unicode chart, page 4. They correspond to Unicode codepoints U+1F550 (CLOCK FACE ONE OCLOCK) through U+1F567 (CLOCK FACE TWELVE-THIRTY).

Your challenge is to write a program that outputs to STDOUT the closest clock face emoji to the current system time (AM and PM should be treated the same). For example if the time t is [5:15 < t < 5:45), you would display the 5:30 emoji 🕠.

This is code-golf, so shortest code in bytes wins. You may want to include a way to easily specify the time to test your code.

Edge test cases

Time   Output  Written
----------------------
11:48  🕛       12:00
3:15   🕞       3:30
9:45   🕙       10:00

enter image description here

\$\endgroup\$
  • 7
    \$\begingroup\$ This question isn't currently on-topic here but it's probably an easy fix. You need an objective win condition, but if you tag the question code-golf the winner will be whoever writes the shortest program. It's also a bit under specified, but you could say "a script that outputs the clock emoji closest to the current time when run" and that should be enough. Also, while not technically required, I'd recommend that you allow submissions in other languages. (Someone will probably do one in Bash anyway.) \$\endgroup\$ – undergroundmonorail Dec 10 '14 at 20:07
  • \$\begingroup\$ Windows Command prompt doesn't support emojis :( \$\endgroup\$ – stokastic Dec 10 '14 at 20:48
  • 2
    \$\begingroup\$ I've edited your post to attempt to have it meet our standards by clarifying the rules and making it code golf. Feel free to edit it if I misinterpreted anything. \$\endgroup\$ – NinjaBearMonkey Dec 11 '14 at 3:44
  • 3
    \$\begingroup\$ How should xx:15 and xx:45 be rounded? \$\endgroup\$ – Peter Taylor Dec 11 '14 at 8:35
  • 1
    \$\begingroup\$ @PeterTaylor As seen in the test cases, they should be rounded up (i.e. 1:45 => 2:00) \$\endgroup\$ – NinjaBearMonkey Dec 11 '14 at 12:53
9
\$\begingroup\$

Ruby — 61 54 bytes

Python 3 — 79 77 75 74 bytes

edit: Turns out this is much shorter in Ruby, because Time doesn't need to be imported and integer division is the default.

a=(Time.now.to_i/900-3)/2%24
puts""<<128336+a/2+a%2*12

Arithmetic:

  • Time.now.to_i: Seconds since epoch.
  • /900: Combine time into 15-minute segments.
  • -3: -4 because characters start at 1 o'clock, +1 so that rounding is done to nearest half hour, rather than downwards.
  • /2: Combine into half-hour segments.
  • %24: Get current half-hour segment out of 12 hours.
  • a/2 + a%2*12: Account for the fact that whole-hour characters come in a block before half-hour characters.

Local time version, 69 bytes:

n=Time.now
a=((n.to_i+n.gmtoff)/900-3)/2%24
puts""<<128336+a/2+a%2*12

Original Python 3 version:

import time
a=int((time.time()/900-3)/2%24)
print(chr(128336+a//2+a%2*12))

Prints the current time UTC. Python 2's chr only accepts 0-255, and unichr only 0-65535. In Python 3, / is always floating-point division and // is always integer division.

\$\endgroup\$
  • \$\begingroup\$ You can cut the parens from 12*(a%2) if you do it in the other order: a%2*12. \$\endgroup\$ – xnor Dec 12 '14 at 5:11
7
\$\begingroup\$

Bash + Coreutils, 60 bytes

date +%I\ 60*%M+45-30/24%%2+2~C*+C8335+0PP|dc|iconv -f ucs-4

Emojis render fine on the OSX terminal, but for Linux (Ubuntu 14.04) you'll have to sudo apt-get install ttf-ancient-fonts

For testing different times, insert -d $time into the date command. e.g for testing 12:59:

date -d 12:59 +%I\ 60*%M+45-30/24%%2+2~C*+C8335+0PP|dc|iconv -f ucs-4
\$\endgroup\$
5
\$\begingroup\$

JavaScript – 137 117 107 102 95

This is going to get easily beaten, but I'll just try my best here.

EDIT 1: Now prints in UTC like the Python answer to save 20 bytes.

EDIT 2: Changed new Date().getTime() to Date.now() to save 10 bytes.

EDIT 3: Changed Math.round(x) to ~~(x+.5) to save 5 bytes.

EDIT 4: Removed unnecessary ( and &1023) left from some old development version where I did a more universal UTF-16 encoding to save 7 bytes.

d=~~(Date.now()/18e5+.5)%24;d+=d<2?24:0;alert(String.fromCharCode(55357,56655+(d%2?23+d:d)/2));

JavaScript does allow for emojis, but in UTF-16 which encodes them as two characters. Luckily, we can simply define them as two characters; the first character is also constant (0xD83D), while the other changes.

Alternative, prints in local time, 137 132 125:

a=new Date();d=~~(a.getHours()%12*2+a.getMinutes()/30+.5);d+=d<2?24:0;alert(String.fromCharCode(55357,56655+(d%2?23+d:d)/2));

\$\endgroup\$
  • \$\begingroup\$ Isn't (d%2?23+d:d) the same as d+d%2*23? \$\endgroup\$ – Neil Feb 27 '17 at 15:31
4
\$\begingroup\$

CJam, 50 bytes

"🕐":i~et3=C%60*et4=+30d/mo2m24+24%_2%24*+2/+]:c

The algorithm is simple, round the time to nearest 30 minutes, count how many 30 minutes have passed since 12 AM/PM, if the number is odd, then add 24 to it, floor divide by 2 and increment the initial unicode by that many code points to get the right clock face.

Also, make sure that you start with 1 AM/PM instead of 12 so take an offset there.

For testing purposes, use the following code:

3:H;45:M;"🕐":i~HC%60*M+30d/mo2m48+48%_2%24*+2/+]:c

Where the 3:H; part is the current hour and the 45:M; part is the current minute of the hour. Simply replace 3 and 45 with the desired value.

Try it online here

Not all unicode characters might print correctly on the online version depending upon your browser and OS and installed fonts. But for me, all print fine on Firefox with Windows 7.

\$\endgroup\$
  • \$\begingroup\$ @user23013 Nice!. I think you should post that as a new answer as I did not do any golfing in that :) \$\endgroup\$ – Optimizer Dec 19 '14 at 14:08
3
\$\begingroup\$

CJam, 31 bytes

"🕐")et5<60b675+30/24%2mdC*++

You can change the time in the following program to test it:

"🕐")[2014 12 19 22 14 1 901 5 -28800000]5<60b675+30/24%2mdC*++
\$\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.