aboutsummaryrefslogtreecommitdiff
path: root/challenge-194/paulo-custodio/cpp/ch-1.cpp
blob: f4b8a6c537440ec91223b03e3fe93d74d0e6157a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Challenge 194

Task 1: Digital Clock
Submitted by: Mohammad S Anwar
You are given time in the format hh:mm with one missing digit.

Write a script to find the highest digit between 0-9 that makes it valid time.

Example 1
Input: $time = '?5:00'
Output: 1

Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place.
Example 2
Input: $time = '?3:00'
Output: 2
Example 3
Input: $time = '1?:00'
Output: 9
Example 4
Input: $time = '2?:00'
Output: 3
Example 5
Input: $time = '12:?5'
Output: 5
Example 6
Input: $time =  '12:5?'
Output: 9
*/

#include <iostream>
#include <string>

int missing_digit(const std::string& clock) {
    if (clock.size()!=5) return -1;
    if (clock[0]=='?' && clock[1]<='3') return 2;
    if (clock[0]=='?') return 1;
    if (clock[0]<='1' && clock[1]=='?') return 9;
    if (clock[1]=='?') return 3;
    if (clock[3]=='?') return 5;
    if (clock[4]=='?') return 9;
    return -1;
}

int main(int argc, char* argv[]) {
    argv++; argc--;
    if (argc != 1) {
        std::cerr << "usage: ch-1 hh:mm" << std::endl;
        return EXIT_FAILURE;
    }

    std::cout << missing_digit(argv[0]) << std::endl;
}