22
\$\begingroup\$

Context

When I was a little kid, I watched a Disney movie where Goofy told his son "Boy, even a broken clock is right two or three times a day." (see this youtube clip, courtesy of @Arnauld).

Rationale

Given the previous statement, we wish to look at a clock that is stopped and answer the following question: is this clock telling the correct time?, -style. But we will also do it Goofy style, assuming a stopped clock gets the correct time 3 times a day.

Task

Pick some time of the morning with at least minute precision. Call it \$t\$.

The function/full program/etc you submit must satisfy the following requirements:

  1. if executed at \$t\text{ am}\$ and at \$t\text{ pm}\$, your code produces an observable output a (think Truthy in challenges).
  2. for every calendar day, there is a third time \$t_1\$ distinct from \$t\text{ am}\$ and \$t\text{ pm}\$ such that, if your code is ran at that time, your code also deterministically produces the observable output a. This means \$t_1\$ may be constant or it may be a function of the day the code is ran at.
  3. if ran at any other time of the day, your code produces an observable output b that must be distinct from a (think Falsy in challenges).

Recall that your code should be precise at least to the minute. This means that you may decide that seconds don't matter for your answer, but you might also want to decide that your answer checks up to nanoseconds in order to decide if it is that time of the day.

Input

Either your code takes no input or it takes the "current" time. For the time, acceptable formats include, but are not limited to:

  • Any ISO format for date/time strings where time is given at least to the minute;
  • An integer list with [hours, minutes] or [hours, minutes, seconds] or any similar list with further subdivisions of the second; (this order may be reversed but may not be shuffled)
  • Different arguments, each representing one element from the lists above.

Output

A single, well-defined, observable output a if your code is run at \$t\text{ am}\$, at \$t\text{ pm}\$, or at \$t_1\$, as specified in your answer. If ran at any other time, your code produces the observable output b, distinct from a.

Bonus imaginary internet points

Bonus imaginary internet points will be awarded to the shortest answer for which \$t_1\$ isn't constant. If this question turns out to receive enough attention, this will turn into an actual rep bounty.


This is so shortest submission in bytes, wins! If you liked this challenge, consider upvoting it! If you dislike this challenge, please give me your feedback. Happy golfing!

\$\endgroup\$
10
  • 15
    \$\begingroup\$ Note that Goofy is right that a stopped clock may get the correct time 3 times a day when Daylight Saving Time ends. \$\endgroup\$
    – Arnauld
    Commented Mar 19, 2020 at 15:30
  • 1
    \$\begingroup\$ @Arnauld indeed! or if he is really really unlucky, only once in the day that DST starts :D \$\endgroup\$
    – RGS
    Commented Mar 19, 2020 at 15:35
  • 1
    \$\begingroup\$ Or the clock could be traveling across time zones :) . @Arnauld \$\endgroup\$ Commented Mar 19, 2020 at 16:44
  • 4
    \$\begingroup\$ @MitchellSpector Yup! Taking +30' and +45' TZ into account, if you travel fast enough and visit the countries in the correct order, you should be able to get the correct time almost 40 times a day. :) \$\endgroup\$
    – Arnauld
    Commented Mar 19, 2020 at 17:02
  • 8
    \$\begingroup\$ ...Goofy actually says "broken" rather than "stopped" which leaves the posibility of a fast or slow running clock too. \$\endgroup\$ Commented Mar 19, 2020 at 17:58

19 Answers 19

19
\$\begingroup\$

JavaScript (ES6), 13 bytes

Takes input as (h)(m). Returns true for 01:00, 02:00 and 13:00, or false for anything else.

h=>m=>26%h<!m

Try it online!

How?

Testing \$26\bmod h<\delta_m\$ is equivalent to test that we have \$m=0\$ and \$26\equiv 0\pmod h\$.

For \$0<h\le23\$, we have \$26\equiv 0\pmod h\$ iff \$h\$ divides \$26\$, i.e. \$h\in\{1,2,13\}\$.

For the edge case \$h=0\$, 26%h results in NaN which is neither greater nor lower than any other value, so the test returns false as expected, no matter the value of \$m\$.

Other possible values

This program generates all possible values \$n<1000\$ for the expression \$n\bmod h\$.

for(n = 1; n < 1000; n++) {
  A = [...Array(24)].map((_, x) => x).filter(x => n % x == 0);
  if(A.length == 3 && A.some(x => A.includes(x + 12))) {
    console.log(n, A);
  }
}

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ This would also work with 85 which has factors 1AM, 5AM, 5PM, right? \$\endgroup\$
    – Sparr
    Commented Mar 20, 2020 at 0:35
  • \$\begingroup\$ Why can't we do (h,m)=>26%h<!m (12 bytes)? \$\endgroup\$ Commented Mar 20, 2020 at 20:24
  • \$\begingroup\$ @DavidCallanan That's 14 bytes. \$\endgroup\$
    – Arnauld
    Commented Mar 20, 2020 at 20:35
  • \$\begingroup\$ @Arnauld Wow I'm bad at counting xD thanks for noticing \$\endgroup\$ Commented Mar 20, 2020 at 20:39
  • 1
    \$\begingroup\$ This is disturbingly brilliant. \$\endgroup\$ Commented Mar 21, 2020 at 4:00
11
\$\begingroup\$

Bash + Core utilities, 26 25 bytes + Bonus imaginary Internet points!

egrep '(.)T(11|23|0\1)00'

Try it online!

Thanks to @mypronounismonicareinstate for pointing out that the ISO 8601 allows omitting the colon in the time part, which saves a byte in the code. (ISO 8601 also allows omitting the hyphens in the date, but that doesn't affect the regex.)

This accepts input on stdin in a format like

20200319T1018-0700

which is a basic ISO 8601 format with precision to the minute.

The result is in the exit code: 0 for truthy (one of the three Goofy times), 1 for falsey (everything else).

The three times it outputs 0 are at:

  • 11:00 am
  • 11:00 pm
  • d:00 am where d is the last digit of the date.

So, today, March 19, it would output truthy (exit code 0) for 9 am (because 9 is the last digit of the date 19), 11 am, and 11 pm.

\$\endgroup\$
4
  • \$\begingroup\$ Without the bonus, I think this works for 20 bytes, outputting 0 at 9:01, 21:01 and 19:01. \$\endgroup\$ Commented Mar 19, 2020 at 22:59
  • \$\begingroup\$ Or even 17 bytes. \$\endgroup\$ Commented Mar 19, 2020 at 23:04
  • \$\begingroup\$ @RobinRyder Looks good to me. If you want to post your 17-byte solution, go ahead. I haven't even looked at answers without the bonus :) . \$\endgroup\$ Commented Mar 19, 2020 at 23:21
  • \$\begingroup\$ No, thanks, go ahead. I wouldn't have thought of it without your answer. :) \$\endgroup\$ Commented Mar 20, 2020 at 7:30
4
\$\begingroup\$

R, 23 bytes

function(h,m)26%%h|m|!h

Try it online!

Port of Arnauld's solution, go upvote his!

Outputs FALSE at 1:00, 13:00 and 2:00, and TRUE otherwise.


Previous version:

R, 26 bytes

function(h,m)!h|m|(h/6)%%1

Try it online!

Outputs FALSE at the three times:

\$t_{am}\$ = 6:00
\$t_{pm}\$ = 18:00
\$t_1\$ = 12:00

and TRUE at any other time.

Indeed, for the output to be FALSE, we need h≠0, m=0, and h to be a multiple of 6.

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 22 \$\cdots\$ 20 19 bytes

Saved a byte thanks to Arnauld!!!

f(h,m){h=26%~h<!m;}

Try it online!

Returns 1 for noon, midnight and 1am, 0 otherwise.
Adaptation of Arnauld's formula.

\$\endgroup\$
4
  • \$\begingroup\$ You can save another byte with 26%~h. \$\endgroup\$
    – Arnauld
    Commented Mar 19, 2020 at 17:27
  • \$\begingroup\$ @Arnauld Just take the mod of the negative. Amazing - thanks! :-) \$\endgroup\$
    – Noodle9
    Commented Mar 19, 2020 at 17:30
  • \$\begingroup\$ Note that in C ISO 1990, the sign of the result of the modulo is implementation-defined (oh dear...). Fortunately, it was changed to the sign of the dividend in C ISO 1999. \$\endgroup\$
    – Arnauld
    Commented Mar 19, 2020 at 17:40
  • 1
    \$\begingroup\$ @Arnauld May take them some time but the standards committees tend to get it right eventually. :D \$\endgroup\$
    – Noodle9
    Commented Mar 19, 2020 at 17:44
3
\$\begingroup\$

Japt -!, 8 bytes

Inspired by Kevin's choice of times. Input as 2 integers in the order h, m. Outputs true at 00:00, 01:01 and 13:01 and false otherwise.

%C*VÉ©Nx

Try it

%C*VÉ©Nx     :Implicit input of integers U=h and V=m
%C           :U modulo 12 (=1 if U=1 or U=13)
  *V         :Multiplied by V (=1 if above and V=1)
    É        :Subtract 1 (=0 if all above)
     ©       :Logical AND with
      N      :Array of inputs (i.e., [U,V])
       x     :Reduced by addition (=0 if U=V=0)
             :Implicit output of boolean negation of result

A port of Arnauld's solution would be 1 byte shorter.

26%U<!V

Try it

\$\endgroup\$
1
  • \$\begingroup\$ I thought this was the shortest... then I realized that these 05AB1E and Jelly solutions aren't upvoted that high. \$\endgroup\$
    – user92069
    Commented Mar 20, 2020 at 6:22
3
\$\begingroup\$

Ruby, 48 44 bytes

-4 bytes thanks to Value Ink!

Differs from other answers so far in that it (i) takes no input and (ii) has a unique \$t_1\$ for every day of the year.

p (t=*Time.now)[2]%12+t[1]<1||t[1,2]==t[3,2]

Try it online! (actual code)

Try it online! (demo version showing the three times that will return true on a given date)

Outputs true at 00:00, 12:00, and month:day (03:20 for today, 20 March); false otherwise.

*Time.now yields an array of the form [sec, min, hour, day, month, year, wday, yday, isdst, zone], of which the first 8 elements are integers (but only min, hour, day, and month are used here). Then print true if hour (mod 12) and min are both 0, or if hour = month and min = day.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Nice first answer here! Good job! You might prefer to include a TIO link for a demo version of your answer; TIO is widespread in this community and is a really nice place to test answers because users can test the answers themselves. \$\endgroup\$
    – RGS
    Commented Mar 20, 2020 at 11:37
  • \$\begingroup\$ @RGS thanks! I can't seem to get TIO to work. I get 'Error: the server's response could not be decoded'. Maybe I'm doing it wrong? \$\endgroup\$
    – Dingus
    Commented Mar 20, 2020 at 11:46
  • \$\begingroup\$ Prob not, some hours ago I also had a problem with TIO. maybe the server is having some trouble... :/ \$\endgroup\$
    – RGS
    Commented Mar 20, 2020 at 11:46
  • 2
    \$\begingroup\$ @RGS I'm having the same issues. Yesterday evening TIO was working 1/4th of the time, but today I get that same connection error every time. Pretty annoying.. (Only positive thing about it, is that I finally decided to install 05AB1E on my local machine, which I had postponed for ages since I have TIO normally xD). And nice first answer, Dingus. Welcome to CGCC! :) \$\endgroup\$ Commented Mar 20, 2020 at 12:15
  • 1
    \$\begingroup\$ Save bytes by splatting Time.now instead of converting to an array: t=*Time.now. Welcome to CGCC! :) \$\endgroup\$
    – Value Ink
    Commented Mar 20, 2020 at 17:07
2
\$\begingroup\$

05AB1E, 16 11 10 bytes

Taking input in the format [minutes, hours]:

`12%*ΘIO_~

Try it online.

Without input and using the current time builtins:

ža12%žb*Θžažb+_~

Try it online (not very useful right now..) or try it online with emulated hours/minutes.

Both programs use the three times:

\$t_{am}\$ = 1:01
\$t_{pm}\$ = 13:01
\$t_1\$ = 00:00

And will output 1 on any of those three times above, and 0 on any other time.

Explanation:

`           # Push the values in the (implicit) input-pair separated to the stack
 12%        # Take modulo-12 on the hours, to make the 24-hour a 12-hour clock
    *       # Multiply it with the minutes
     Θ      # And check if it's exactly 1 (1 if truhy; 0 if falsey)
I           # Push the input-pair again
 O          # Sum them together
  _         # Check if it's exactly 0 (1 if truhy; 0 if falsey)
      ~     # And then check if either of the two checks is 1
            # (after which the result is output implicitly)

ža          # Push the current hours
  12%       # Modulo 12, to make the 24-hour a 12-hour clock
     žb     # Push the current minutes
       *    # Multiply them together
        Θ   # And check if it's exactly 1 (1 if truthy; 0 if falsey)
žažb        # Push the current hours and minutes again
    +       # Add them together
     _      # Check if it's exactly 0 (1 if truthy; 0 if falsey)
         ~  # And then check if either of the two checks is 1
            # (after which the result is output implicitly)
\$\endgroup\$
2
  • \$\begingroup\$ Interesting solution :) I take it you already had it prepared? Or did you come up with all of this just now? \$\endgroup\$
    – RGS
    Commented Mar 19, 2020 at 14:28
  • \$\begingroup\$ @RGS Came up with it just now. :) And -5 bytes by taking the hours/minutes as input. May we take the input as [minutes, hours] instead of [hours, minutes]? \$\endgroup\$ Commented Mar 19, 2020 at 14:29
2
\$\begingroup\$

05AB1E (legacy), 5 bytes

_₂I%›

Port of @Arnauld's JavaScript answer, so make sure to upvote him!!

Takes two loose inputs, in the order minutes, hours.

Will also output 1 for any of these three times below, and 0 on any other time:

\$t_{am}\$ = 01:00
\$t_{pm}\$ = 13:00
\$t_1\$ = 02:00

Try it online or verify some more test cases.

Explanation:

_     # Check if the (implicit) input-integer (minutes) is 0 (1 if truthy; 0 if falsey)
 ₂    # Push 26
  I   # Push the second input-integer (hours)
   %  # Take 26 modulo-hours (will result in 26 if the hours are 0 in the legacy version)
    › # Check if the first value is larger than the second
      # (after which it is output implicitly as result)
\$\endgroup\$
2
  • \$\begingroup\$ How in the world did Adnan think of the 26 constant already? \$\endgroup\$
    – user92069
    Commented Mar 20, 2020 at 6:28
  • \$\begingroup\$ @a'_' Dunno. Probably because there are 26 letters in the alphabet. Although I'm wondering the same thing about some of the other single-byte constants tbh, since we also have 36,95,255,256 as single-byte builtins (and 10,100,1000 of course). \$\endgroup\$ Commented Mar 20, 2020 at 7:42
2
\$\begingroup\$

Python 3, 21 bytes

lambda h,m:26%-~h<m<2

Try it online!

Input: hour and minute, as integers
Output: True if the time is 00:01,01:01,12:01, otherwise False.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 35 \$\cdots\$ 22 19 bytes

Saved a byte thanks to Arnauld!!!
Saved 3 bytes thanks to Surculose Sputum!!!

lambda h,m:26%~h==m

Try it online!

Returns True for noon, midnight and 1am, False otherwise.
Adaptation of Arnauld's formula.

\$\endgroup\$
1
  • \$\begingroup\$ Do you need the +(...)? I think outputting True or False would work. \$\endgroup\$ Commented Mar 19, 2020 at 17:49
1
\$\begingroup\$

Retina 0.8.2, 28 bytes

^.
$&$&$&$&
\G\d
$*
^1{8}:18

Try it online! Link includes test cases. Explanation:

^.
$&$&$&$&

Repeat the hour tens digit four times.

\G\d
$*

Convert the hour digits to unary.

^1{8}:18

They must sum to 8 and the minutes must be 18 (arbitrarily chosen to reuse the same digits).

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

Charcoal, 10 bytes

⁼²№⟦↨S⁴N⟧⁹

Try it online! Link is to verbose version of code. Takes input as hour and minute separated by a space, on separate lines, or as a JSON list i.e. [22, 10], and outputs - for 09 09, 15 09 and 21 09 only. Explanation:

   ⟦    ⟧   List of
     S      Hours as a string
    ↨ ⁴     Interpreted as base 4
       N    Minutes
  №      ⁹  Count `9`s
⁼²          Must be 2 of them.
\$\endgroup\$
1
\$\begingroup\$

Ruby, 19 bytes

Returns true at 6:00, 12:00, 18:00.

->h,m{h>0&&m|h%6<1}

Try it online!

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

Jelly,  7  6 bytes

Turns out porting Arnauld's answer is a byte shorter than my original answer (which I've kept below)

26%<¬}

A dyadic Link accepting the hour (an integer in [0,23]) on the left and the minute (an integer in [0,59]) on the right which yields 1 at three times (not varying by day), 2,0 (02:00), 1,0 (01:00), and 13,0 (13:00).

Try it online!


My 7

æle1,98

A dyadic Link accepting the hour (an integer in [0,23]) on the left and the minute (an integer in [0,59]) on the right which yields 1 at three times (not varying by day), 1,1 (01:01), 2,49 (02:49), and 14,49 (14:49).

Try it online!

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

perl -ple, 18 bytes

$_=/(2[01]|08):00/

Reads a time in HH:MM format from STDIN (optionally preceded by a date in YYYY-MM-DD format (or anything else which doesn't like a time)).

Print 1 followed by a newline if the time is 08:00, 20:00 or 21:00; otherwise, it prints a single newline.

\$\endgroup\$
1
  • \$\begingroup\$ Hey there, welcome to this community! Your submission looks nice but I don't really know Perl! Do you think you could include a TIO link? If you check the other answers, you will notice they all include such a link, usually with the text "Try it online" :) \$\endgroup\$
    – RGS
    Commented Mar 19, 2020 at 20:28
1
\$\begingroup\$

W, 5 bytes

... With True and False swapped around; they're distinctive anyway.

♥─y¥•

Uncompressed:

!26bm>

Explanation

!      % Check whether the input is equal to 0
   b   % Push the second input
 26 m  % Push 26 % b
     > % Is input == 0 larger than the above value?
\$\endgroup\$
1
\$\begingroup\$

Retina, 18 17 bytes + Bonus imaginary Internet points

(.)T(11|23|0\1)00

Try it online!

Note that TIO is having technical issues at the moment, so the link that it gave me above may or may not work.


Thanks to @mypronounismonicareinstate for pointing out that the ISO 8601 allows omitting the colon in the time part, which saves a byte in the code. (ISO 8601 also allows omitting the hyphens in the date, but that doesn't affect the regex.)


I don't really know Retina, but this port of my bash answer appears to be right :) .

The program accepts input on stdin in a format like

20200319T1018-0700

which is a basic ISO 8601 format with precision to the minute.

Like my bash answer, it outputs truthy (0) for 11:00 am, 11:00 pm, and d:00 am where d is the last digit of the current date. It outputs falsey (1) otherwise.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice job getting the bonus imaginary internet points!! :D \$\endgroup\$
    – RGS
    Commented Mar 19, 2020 at 19:27
  • \$\begingroup\$ I'm pretty sure the : and the - are not necessary in ISO 8601 (posssibly saving 1 byte). \$\endgroup\$ Commented Mar 20, 2020 at 8:42
  • \$\begingroup\$ @mypronounismonicareinstate That's interesting, thank you. I hadn't checked out the spec -- I just went off of what GNU date printed for its ISO 8601 output. \$\endgroup\$ Commented Mar 20, 2020 at 8:44
1
\$\begingroup\$

Perl 5 -lF: , 34 bytes

say$F[0]%12&&$F[0]!=1||$F[1]>0?0:1

Try it online!

Reads current time input from stdin in hh:mm format. Outputs 1 at 00:00, 01:00, and 12:00; 0 at all other times.

\$\endgroup\$
2
  • \$\begingroup\$ -F implies -n and -a, so you can reduce your command line options to -lF:. \$\endgroup\$
    – Abigail
    Commented Mar 19, 2020 at 20:36
  • \$\begingroup\$ That's only true since 5.20.0, but TIO is using a later version so I'll take it - thanks :) \$\endgroup\$
    – andytech
    Commented Mar 20, 2020 at 12:48
0
\$\begingroup\$

Io, 19 bytes

Port of @Arnauld's answer...

method(U,V,26%U>=V)

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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