There are many puzzles with matches that involve adding, removing, or moving a certain number of matches to create new numbers or shapes. This is like that with a digital clock.
Given a valid time on a 12-hour digital clock, output the digit that requires moving the fewest lines to make it so every visible digit on the clock becomes that digit. If more than one digit is the minimum, output them all. If it is impossible to make every digit the same, output -1
or a falsy value other than 0 (you'll get a lot of these).
The clock digits look like this:
|
|
_
_|
|_
_
_|
_|
|_|
|
_
|_
_|
_
|_
|_|
_
|
|
_
|_|
|_|
_
|_|
_|
_
| |
|_|
Test Cases:
Input: 123
Clock Display:
_ _
| : _| _|
| : |_ _|
Output: 4
Explanation: The display for 1:23
requires a total of 12 lines to be drawn. Therefore, for every digit to be the same, each digit would have to have 4 lines. The only digit that has 4 lines is 4
. Therefore, the answer has to be 4
.
Input: 1212
Clock Display:
_ _
| _| : | _|
| |_ : | |_
Output: -1
Explanation: The display for 12:12
requires 14 lines. 14 divided by 4 is not an integer, therefore it is impossible for every digit to be the same.
Input: 654
Clock Display:
_ _
|_ : |_ |_|
|_| : _| |
Output: 5
Explanation: The total number of lines is 15. 15 divided by 3 is 5, so each digit must have 5 lines. The only digits that have 5 lines are 2
,3
, and 5
. The answer is 5
because it only requires 2 moves to make every digit 5. Simply move the line at the bottom left of the 6 to the bottom of the 4, then you have:
_ _
|_ : |_ |_|
_| : _| _|
Then, as you can see, all you need to do is move the line at the top right of the digit that was originally 4 to the top, and you get 5:55
. To make every digit a 2
or 3
would require more than 2 moves.
Input: 609
Clock Display:
_ _ _
|_ : | | |_|
|_| : |_| _|
Output: 609
(6,0,9
or [6,0,9]
also ok).
Explanation: 6
, 0
, and 9
are the only digits that have 6 lines. As such, they are also the only possible solutions. It's not hard to see that it would take two moves to make any of these the only digit. Therefore, you output all three digits.
Notes:
- Although the input time must be valid, the output time does not (e.g.
999
as an output is OK.) - I am very flexible with input. You can require a leading 0. You can use a number with a decimal point. You can use a string. You can use an array. You can have a parameter for every digit.