7
\$\begingroup\$

Related to, but not a duplicate of, this challenge.

Basics

This is a clock:

              ******
          **     |    **
      **         |        **
    *            |           *
    *            |           *
  *              |             *
  *              |             *
*                |               *
*                |               *
*                |_______        *
*                                *
*                                *
*                                *
  *                            *
  *                            *
    *                        *
    *                        *
      **                  **
          **          **
              ******

Let's note some of the basic properties:

  1. It's a circle, 40*20 (width*height) characters, made up of asterisks.
  2. There are two hands: a minute hand made of 9 pipe characters (|); and an hour hand made of 7 underscores (_). I.e., in this example the time is 3:00

If a clock weren't arranged in right angles, it might look something like this:

              ******
          **          **
      **  |               **
    *      |                 *
    *       |                *
  *          |                 *
  *           |                *
*              |                 *
*               |                *
*                |_              *
*                   _            *
*                     _          *
*                       _        *
  *                            *
  *                            *
    *                        *
    *                        *
      **                  **
          **          **
              ******

This represents 4:55.

What's happened here is:

  1. We determine the destination (see the diagram below).
  2. We determine the number of spaces to delimit by. This will either be 0, 1, or 2 (see the table below).
  3. We draw the line by using the respective character (| for minute, _ for hour).
  4. We then cut off the line at the number of characters (9 for minute, 7 for hour), including spaces, from the center of the circle along the x-axis. If we reach the edge of the clock, we stop anyway:
              ******
       |  **          **
      *|  |               **
    *  |   |                 *
    *  |    |                *
  *    |     |                 *
  *    |      |                *
*      |       |                 *
*      |        | 1234567|       *
*      |         |_      |       *
*      |987654321   _    |       *
*                     _  |       *
*                       _|       *
  *                            *
  *                            *
    *                        *
    *                        *
      **                  **
          **          **
              ******

For the sake of completeness, here's a diagram of all possible angles. This applies to minute-hands too: they won't go more specific than 5-minute intervals.

              **→0←*
          ↓*          ** ↓
      ** →11             1←*
    *                        *
    *↓                      ↓*
  * →10                     2← *
  *                            *
*                                *
*                                ↓
↓               •                3
9                                ↑
↑                                *
*                                *
  *                            *
  * →8                      4← *
    *↑                      ↑*
    *                        *
      ** →7              5←*
          ↑*          ** ↑
              *→6←**

The number of spaces to reach each point is as follows:

+-------+----------------------------------------+
| Point | No. of spaces to delimit with to reach |
+-------+----------------------------------------+
| 1     | 1                                      |
+-------+----------------------------------------+
| 2     | 2                                      |
+-------+----------------------------------------+
| 3     | 0                                      |
+-------+----------------------------------------+
| 4     | 2                                      |
+-------+----------------------------------------+
| 5     | 1                                      |
+-------+----------------------------------------+
| 6     | 0                                      |
+-------+----------------------------------------+
| 7     | 1                                      |
+-------+----------------------------------------+
| 8     | 2                                      |
+-------+----------------------------------------+
| 9     | 0                                      |
+-------+----------------------------------------+
| 10    | 2                                      |
+-------+----------------------------------------+
| 11    | 1                                      |
+-------+----------------------------------------+
| 0     | 0                                      |
+-------+----------------------------------------+

Note that it is asymmetrical. However, if you wish, you can also take input with the clock flipped vertically, horizontally or both, e.g.

              *→○←**
         ↓**          **↓
      **→○              ○←**
    *                        *
    *↓                      ↓*
  * →○                      ○← *
  *                            *
*                                *
*                                ↓
↓                •               ○
○                                ↑
↑                                *
*                                *
  *                            *
  * →○                      ○← *
    *↑                      ↑*
    *                        *
      **→○              ○←**
         ↑**          **↑
              **→○←*

(flipped horizontally)

Similarly, when two hands overlap (see below) both hands would usually be aligned to the top of the top-left of the four centre squares. When requesting a flipped input, the centre location will also be flipped accordingly. Note that you cannot mix the two: if you want a flipped input, all aspects, including the centre-point and the angle anchors, will be flipped. (On these example diagrams, the centre-point is represented with the centre dot )

For this challenge:

  • The minute hand will always be a multiple of five
  • The hour hand will always be on the base of the hour (e.g. even if it is 5:59, the hour hand will be exactly on 5)
  • Only valid times will be given, and the size of the clock will be constant
  • If the hour hand and minute hand are on the same line, the hour hand (_) will be used for the first seven characters, followed by | for two more, e.g.:
              ******
          **          **
      **                  **
    *                        *
    *                        *
  *                            *
  *                            *
*                                *
*                                *
*               _                *
*             _                  *
*           _                    *
*         _                      *
  *     |                      *
  *                            *
    *                        *
    *                        *
      **                  **
          **          **
              ******

would represent 8:40.

Challenge

Given a clock in this format, output the time displayed on the clock. The time should be in 12-hour format (i.e. not starting at 12), but the top number can be 0 or 12, as you wish.

Rules

  • You can return the time as an array of [hours, minutes], a string delimited by some character, or any other reasonable format. You must return pure, base-10 numbers, though: hex representations (0-A instead of 0-11) or other replacements for numbers are disallowed.
  • Results can be padded by whitespace or zeros if you wish (e.g. 01:12 instead of 1:12)
  • The characters mentioned here must be the ones used by your program. Your submission cannot require substitution of these characters for something else, e.g. take a clock with '#' for the hour hand. Further, the input will always contain the spaces at the start of lines, but spaces after the clock (on the right of the diagram) are optional, e.g.:
              ******              
          **     |    **          
      **         |        **      
    *            |           *    
    *            |           *    
  *              |             *  
  *              |             *  
*                |               *
*                |               *
*                |_______        *
*                                *
*                                *
*                                *
  *                            *  
  *                            *  
    *                        *    
    *                        *    
      **                  **      
          **          **          
              ******              

(select this!)
\$\endgroup\$
5
  • 1
    \$\begingroup\$ @JonathanAllan The minute hand is 9 characters long if you include the spaces (see the diagram on step 4). Note that there's two spaces here instead of one, which may be the cause of the confusion. I was trying to figure out your second point for a while, because I couldn't figure out the source of the issue; turns out, I just needed a clarification in the rules about the alignment. \$\endgroup\$ May 10, 2020 at 21:50
  • \$\begingroup\$ I'm still unsure about the rules dictating where the central-most characters are displayed - the central-most hour character is not on top of the central-most minute character in the 3:00 or the 4:55 examples, yet it is in the 8:40 one. ("hands would usually be aligned to the top of the top-left of the four centre squares." - is that true for 3:00 and 4:55?) \$\endgroup\$ May 10, 2020 at 22:12
  • \$\begingroup\$ @JonathanAllan See now. \$\endgroup\$ May 11, 2020 at 8:00
  • \$\begingroup\$ I'm confused by the center dot in combination with the 8:40 example and positions of the 6 and 9 o'clock os in your graph.. Being an asymmetrical circle, it seems there are three different centers to start from - where the hours 6, 8, and 9 differ from that dot , unless I'm doing something wrong? Here a pastebin of how I currently understand the hour and minute arms of each time. I'm pretty sure it's incorrect, but I'm not sure what the intended arms should be instead.. \$\endgroup\$ May 11, 2020 at 11:33
  • \$\begingroup\$ I cannot work out the rules of the central-most characters still. Maybe a link to code that produces a clock given the time would help here? \$\endgroup\$ May 11, 2020 at 16:33

1 Answer 1

2
\$\begingroup\$

05AB1E (legacy), 51 50 bytes

•4D-aÕΩ/Ȱøcû'a•žAвè'_kI•5₁¦M5₅϶ÊäÄVd‰•558вè'|k5*‚

Input as a single multi-line string without trailing spaces; output as a pair of [hours, minutes].

-1 byte by switching to the legacy version, where the max/min works on character-lists, based on the ordinal codepoint values of the characters. (In the new version it will simply return the first character instead..)

Try it online or verify all test cases.

Assumes these below are all the clock arms:

              ******
          **     |    **
      **  |      |      | **
    *      |     0     1     *
    *       B    0    1      *
  *     |    B   0   1   |     *
  *       A   B  0  1  2       *
*           A  B 0 1 2           *
*             A B012             *
*               yx333333||       *
*      ||99999##z 5 4            *
*           8 7 6  5  4          *
*         8  7  6   5   4        *
  *     |   7   6    5    |    *
  *        7    6     5        *
    *     7     6      5     *
    *    |      6       |    *
      **        |         **
          **    |     **
              ******

Where:

  • 0-B represents the hour-arms for the hours 0 to 11;
  • | represents the tips of the minute-arms;
  • xyz represents the -multiple- cents, where x is the center for 0,1,2,3,4,5,11; y is the center for 7,8,10; and z is the center for 6,9;
  • # are overlapping hour-numbers (for 7&9 and 8&9 respectively).

If they are incorrect (which is likely, since I have multiple centers, and the challenge description is a bit confusing tbh..), the code can easily be modified to correct the arms. The approach will remain the same, I'd just have to adjust the indices in the compressed lists in that case.

Explanation:

Checks at the following positions in the input-string:

              ******
          **          **
      **  l      a      b **
    *                        *
    *       L    A    B      *
  *     k                c     *
  *       K            C       *
*                                *
*                                *
*                      D d       *
*      j J                       *
*                                *
*         I             E        *
  *     i                 e    *
  *                            *
    *     H     G      F     *
    *    h              f    *
      **        g         **
          **          **
              ******

It will first get a list of all characters in the uppercase positions of the graph above (in alphabetical/clock order), and will use the first 0-based index in this list where it finds a '_' as the hour. It will do the same with the lowercase positions and '|', after which it will multiply this by 5 as the minutes.

•4D-aÕΩ/Ȱøcû'a•  '# Push compressed integer 78119436285968343280298302466166
  žAв              # Converted to base-512 as list: [123,128,193,296,402,502,495,489,388,317,180,118]
     è             # Get the characters at those indices in the (implicit) input-string
      '_k         '# Get the first index of an '_'
•5₁¦M5₅϶ÊäÄVd‰•   # Push compressed integer 103082336338078574425677583851482
  558в             # Converted to base-558 as list: [63,70,162,298,439,534,557,519,421,315,145,56]
I     è            # Get the characters at those indices in the input-string
       Z           # Get the maximum character in this list (without popping the list),
                   # which will always be the '|'
        k          # Get the index of this '|' in the list
         5*        # Multiply that by 5
‚                  # And pair both integers together
                   # (after which it is output implicitly as result)

See this 05AB1E tip of mine (sections How to compress large integers? and How to compress integer lists?) to understand why •4D-aÕΩ/Ȱøcû'a• is 78119436285968343280298302466166; •4D-aÕΩ/Ȱøcû'a•žAв is [123,128,193,296,402,502,495,489,388,317,180,118]; •5₁¦M5₅϶ÊäÄVd‰• is 103082336338078574425677583851482; and •5₁¦M5₅϶ÊäÄVd‰•558в is [63,70,162,298,439,534,557,519,421,315,145,56].

\$\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.