9
\$\begingroup\$

Introduction:

Some times using a 24-hour clock are formatted in a nice pattern. For these patterns, we'll have four different categories:

All the same (pattern A:AA or AA:AA):
 0:00
 1:11
 2:22
 3:33
 4:44
 5:55
11:11
22:22
Increasing (pattern B:CD or AB:CD, where D==C+1==B+2==A+3):
 0:12
 1:23
 2:34
 3:45
 4:56
12:34
23:45
Pair (pattern AB:AB):
10:10
12:12
13:13
14:14
15:15
16:16
17:17
18:18
19:19
20:20
21:21
23:23
Palindrome (pattern A:BA or AB:BA):
 0:10
 0:20
 0:30
 0:40
 0:50
 1:01
 1:21
 1:31
 1:41
 1:51
 2:02
 2:12
 2:32
 2:42
 2:52
 3:03
 3:13
 3:23
 3:43
 3:53
 4:04
 4:14
 4:24
 4:34
 4:54
 5:05
 5:15
 5:25
 5:35
 5:45
 6:06
 6:16
 6:26
 6:36
 6:46
 6:56
 7:07
 7:17
 7:27
 7:37
 7:47
 7:57
 8:08
 8:18
 8:28
 8:38
 8:48
 8:58
 9:09
 9:19
 9:29
 9:39
 9:49
 9:59
10:01
12:21
13:31
14:41
15:51
20:02
21:12
23:32

Challenge:

Given a starting time and a category, output how many minutes should be added to the start time, for it to reach the closest (forward) time of the given category.

For example: if the start time is 14:47 and the category is palindrome, the smallest time after 14:47 in the palindrome category is 15:51, so the output is 64 minutes (because 14:47 + 64 minutes = 15:51).

Challenge Rules:

  • As you may have noted: the times in the 'All the same' category are excluded from the 'Pair' and 'Palindrome' categories, even though they are technically also pairs/palindromes.
  • We only look forward when we go to the closest time. So in the example above the output is 64 (time 15:51) and not -6 (time 14:41).
  • You may take the times in any reasonable format. May be a string, a (Date)Time-object, a pair of integers, etc.
  • You may use any reasonable distinct input-values for the four categories. (Keep in mind this forbidden loophole!) May be four integers; may be the category-names as strings; may be an enum; etc.
  • If the input-time is already valid for the given category, the result is 0 (so we won't look for the next one).
  • We use an A:BC pattern for single-digit hours, so they won't be left-padded with a 0 to 0A:BC.

General Rules:

  • This is , so the shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code (e.g. TIO).
  • Also, adding an explanation for your answer is highly recommended.

Test Cases:

You don't need to output the time between parenthesis. Those are just added as clarification in the test cases below. Only the integer-output is required to be output.

Inputs: 14:47, palindrome
Output: 64   (15:51)

Inputs: 0:00, all the same
Output: 0    (0:00)

Inputs: 23:33, palindrome
Output: 37   (0:10)

Inputs: 23:24, pair
Output: 646  (10:10)

Inputs: 10:00, increasing
Output: 154  (12:34)

Inputs: 0:00, increasing
Output: 12   (0:12)

Inputs: 23:59, all the same
Output: 1    (0:00)

Inputs: 11:11, pair
Output: 61   (12:12)
\$\endgroup\$
9
  • \$\begingroup\$ "Don't let code-golf languages discourage you from posting answers with non-codegolfing languages." That's exactly what discourages to post answers. Opinions aside, can you provide a few test cases of expected outputs? Also, you have "May be four integers; may be the category-names as strings; may be an enum; etc." but then you say "We use an A:BC pattern for single-digit hours, so they won't be left-padded with a 0 to 0A:BC.". Does this mean the input, as numbers, has to be [0,1,2,3] or [1,2,3] for a time of "1:23" as a string? Or I'm mixing stuff up? \$\endgroup\$ Oct 20, 2022 at 11:00
  • \$\begingroup\$ @IsmaelMiguel "can you provide a few test cases of expected outputs?" There are 6 test cases at the bottom.. Any suggested test case I should add? And for input as numbers, I meant it as a pair of [hours,minutes] (or two loose hours and minutes inputs) - e.g. [1,23] instead of 1:23. Although I guess [1,2,3] instead of 1:23 is valid as well if you want. \$\endgroup\$ Oct 20, 2022 at 11:10
  • \$\begingroup\$ I started to write the comment before your edit. But I got distracted and only finished it later. Didn't notice the edit after submitting. Can we assume that all inputs are valid? Also, a test case could be 23:59. Does it output 0:00 (1) for "all the same" and 0:10 (11) for "palindrome"? Or it isn't supposed to wrap around? \$\endgroup\$ Oct 20, 2022 at 11:34
  • 1
    \$\begingroup\$ Suggested test case: 11:11, pair ~> 61. \$\endgroup\$
    – Arnauld
    Oct 20, 2022 at 13:39
  • 1
    \$\begingroup\$ @Arnauld Changed the title ('Time Patterns' was too short). \$\endgroup\$ Oct 21, 2022 at 6:28

5 Answers 5

5
\$\begingroup\$

Pyth, 82 bytes

L+%/b60 24%"%02d"%b60JiQ60KE-f@[q1l{yT!-.+CMyT1&%%T60 11q.*cyT2&%%T60 11q_yTyT)KJJ

Try it online!

A bit ham fisted, but it works. Expects time as an integer list [hours, minutes] and type as (ALL, INC, PAI, PAL) -> (0, 1, 2, 3)

Explanation

L+%/b60 24%"%02d"%b60                                                                 define y(b) to format a number of seconds to the string "(h)hmm"
                     JiQ60                                                            set J to the input time as a number of seconds
                          KE                                                          set K to the input mode
                            -                                                    J    subtract the input time from
                             f                                                  J     the first integer greater than or equal to the input which satisfies
                              @[                                              )K      mode selection
                                q1l{yT                                                formatted string only has one unique character (all)
                                      !-.+CMyT1                                       code point deltas are all 1 (inc)
                                               &%%T60 11q.*cyT2                       string splits into two equal parts but seconds are not 0 mod 11 (pai)
                                                               &%%T60 11q_yTyT        string is the same as itself backwards but seconds are not 0 mod 11 (pal)
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 126 bytes

-2 bytes thanks to Kevin

Expects (type)(minutes, hours), with type in \$[0..3]\$ (all the same, increasing, pair and palindrome respectively).

t=>g=(m,h,T=10,d=m-h)=>[q=m%11|d&&d-h*T,d-12-T*(282%h^6?h:1),h<T|d,(h>9?m/T^h%T?T:h/T:h)^m%T][t]|!q*t&&1+g(m=-~m%60,(h+!m)%24)

Try it online!

How?

All formulae are falsy if the criterion is met, or truthy if it's not.

If the first test is triggered, the other ones are forced to fail (this is | !q * t in the code). This enforces the rule about discarding pairs and palindromes with a unique digit.

All the same

m % 11 | m - h && m - h * 11

We must have either:

  • \$m\bmod 11=0\$ and \$m=h\$ (e.g. 22:22)
  • or \$m=h\times11\$ (e.g. 4:44)

Increasing

m - h - 12 - 10 * (282 % h ^ 6 ? h : 1)

We must have either:

  • \$m=h+12+10\$ if \$h\in\{12,23\}\$
  • or \$m=h+12+10\times h\$ if \$h\notin\{12,23\}\$

We have \$282\bmod h=6\$ if \$h\in\{12,23\}\$ and \$282\bmod h\neq 6\$ otherwise.

Pair

h < 10 | h - m

We must have \$h\ge9\$ and \$h=m\$.

Palindrome

(h > 9 ? m / 10 ^ h % 10 ? 10 : h / 10 : h) ^ m % 10

We must have either:

  • \$h=m\bmod 10\$ if \$h\le 9\$
  • or \$\lfloor m/10\rfloor = h\bmod 10\$ and \$\lfloor h/10\rfloor = m\bmod 10\$ if \$h>9\$
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 129 bytes by creating a variable for the seven occurrences of 10. Nice answer btw! \$\endgroup\$ Oct 20, 2022 at 14:15
  • \$\begingroup\$ your inequality symbols seem backwards in the pair and palindrome explanations, but im not sure \$\endgroup\$ Oct 20, 2022 at 16:54
  • \$\begingroup\$ @thejonymyster The explanations tell what we must have for a valid pattern. On the other hand, as mentioned at the very beginning of the explanation, the code is thuthy when the pattern is invalid. (For instance, !(h<10|h-m) is equivalent to h>=9&h==m.) \$\endgroup\$
    – Arnauld
    Oct 20, 2022 at 16:59
4
\$\begingroup\$

Jelly, 33 bytes

,_J$;,Ṛ,ŒHƲE€i1
“𩽑ḶŒpDF€ṙ⁸Ç€i’

A dyadic link that accepts the time of day in minutes as an integer on the left and the category as an integer on the right and yields the number of minutes until the clock will show a time with the given category.

category input value
all the same 1
increasing 2
palindrome 3
pair 4

(Zero will give the time until no category too.)

Try it online! Or see the test-suite.

How?

,_J$;,Ṛ,ŒHƲE€i1 - Helper Link = categorise: Time as a list of three or four digits
   $            - last two links as a monad - f(Time):
  J             -   range of length (Time) -> [1,2,3] or [1,2,3,4]
 _              -   (Time) subtract (that) (vectorises)
,               - (Time) pair with (that)
          Ʋ     - last four links as a monad - f(Time):
      Ṛ         -   reverse (Time)
     ,          -   (Time) pair with (that)
        ŒH      -   split (Time) into two halves
       ,        -   ([Time, Reversed(Time)]) pair (that)
    ;           - concatenate
                  -> [Time, Time - Indices, [Time, Reversed(Time)], [Prefix, Suffix]]
            €   - for each:
           E    -   all equal? -> 1 or 0
             i1 - first 1-indexed index of 1, or 0 if none

“𩽑ḶŒpDF€ṙ⁸Ç€i’ - Link = time to next: time of day in minutes, M; category, C
“𩽑             - code-page indicies = [24,6,10]
     Ḷ            - lowered range -> [[0..23],[0..5],[0..9]]
      Œp          - Cartesian product -> [[0,0,0],...,[0,5,9],[1,0,0],...,[23,5,9]]
        D         - digits -> [...,[[2,3],[5],[9]]]
         F€       - flatten each -> [[0,0,0],...,[0,5,9],[1,0,0],...,[2,3,5,9]]
           ṙ⁸     - rotate (that) left by (M)
             ǀ   - call the helper Link (above) for each
               i  - first 1-indexed index of (C) in (that) 
                ’ - decrement
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 70 bytes

I⌕EE﹪⁺↨⁶⁰↨N¹⁰⁰…⁰φ¹⁴⁴⁰﹪%03d↨¹⁰⁰↨ι⁶⁰⌕⟦⁼⌊ι⌈ι⬤ι∨¬μ⁼λI⊕§ι⊖μ⁼⌊⪪ι²⌈⪪ι²⁼ι⮌ι⟧¹N

Try it online! Link is to verbose version of code. Takes the time as a 3- or 4-digit string (e.g. 2324) and maps the categories to 0..3 for same, increasing, pair, palindrome. Explanation:

⁺↨⁶⁰↨N¹⁰⁰

Convert the input time into a number of minutes since midnight.

﹪...…⁰φ¹⁴⁴⁰

Create a range of 1000 minutes starting that that time, and wrapping around at midnight if necessary.

E...﹪%03d↨¹⁰⁰↨ι⁶⁰

Convert each time into that range back into a 3- or 4-digit string as appropriate.

E...⌕⟦...⟧¹

Find the category of each time, given as the index of the first of these expressions that evaluates to true: ...

⁼⌊ι⌈ι

... all the digits are the same; ...

⬤ι∨¬μ⁼λI⊕§ι⊖μ

... each digit is one more than the previous; ...

⁼⌊⪪ι²⌈⪪ι²

... the time is composed of two equal pairs; or...

⁼ι⮌ι

... the time equals its reverse.

I⌕...N

Find the index of the first time in the desired category.

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

Japt, 73 bytes

@V¥[_e¥0}_e¥J}_ä+ e[TT]}_Î¥ZÌn «Zx}]b!gUì60_+60²+XÃÅvu24 gJ,_s ù'02ìä-}a

Try it

Input is [hours, minutes], pattern with pattern in the range [0...3] using the same order as they are listed in the question.

Explanation:

Broad overview:

@                 }a # Find the smallest X that returns true:
           Uì...ä-   #  Calculate the time after X minutes and do some preprocessing
   [...]b!g          #  Find the first pattern that it matches
 V¥                  #  Check that it's the pattern we're looking for

Calculating times:

Uì60_+60²+XÃÅvu24 gJ,_s ù'02ìä-
Uì60_                            # Convert the input from base-60 to base-10
     +60²                        # Add 3600, forcing 3 digits of output
         +X                      # Add X minutes
           Ã                     # Convert back to base-60
            Å                    # Remove the extra digit we added
             v                   # Modify the first remaining digit:
              u24                #  Mod 24
                  gJ,_      Ã    # Modify the last digit:
                      s          #  Convert to a string
                        ù'02     #  Left-pad with 0 to length 2
                             ¬   # Join the digits to a string
                              ä- # Get the difference between consecutive digits

Check against the patterns:

[_e¥0}_e¥J}_ä+ e[TT]}_Î¥ZÌn «Zx}]b!g
                                  !g # Consecutive differences as Z
[                               ]b   # Index of first function that returns true:

 _   }                               #  Index 0: All the same
  e                                  #   Every item in Z
   ¥0                                #   Is 0

      _   }                          #  Index 1: Increasing
       e                             #   Every item in Z
        ¥J                           #   Is -1

           _        }                #  Index 2: Pair
            ä+                       #   Consecutive sums of Z
               e[TT]                 #   Is exactly [0,0]

                     _         }     #  Index 3: Palindrome
                      Î              #   The first item in Z
                       ¥  n          #   Is the opposite of
                        ZÌ           #   The last item in Z
                            «Zx      #   And the sum of Z is 0
\$\endgroup\$

Your Answer

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

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