Planet Lapituletti (Codeforces 1493B)
Tags: Implement, brute force

The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts $$$h$$$ hours and each hour lasts $$$m$$$ minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from $$$0$$$ to $$$h-1$$$ and minutes are numbered from $$$0$$$ to $$$m-1$$$.  That's how the digits are displayed on the clock. Please note that digit $$$1$$$ is placed in the middle of its position. A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).The image of the clocks in the mirror is reflected against a vertical axis.  The reflection is not a valid time.The reflection is a valid time with $$$h=24$$$, $$$m = 60$$$. However, for example, if $$$h=10$$$, $$$m=60$$$, then the reflection is not a valid time. An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment $$$s$$$ and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.It can be shown that with any $$$h$$$, $$$m$$$, $$$s$$$ such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.You are asked to solve the problem for several test cases.

Input: The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of test cases.The next $$$2 \cdot T$$$ lines contain the description of test cases. The description of each test case consists of two lines.The first line of a test case contains two integers $$$h$$$, $$$m$$$ ($$$1 \le h, m \le 100$$$).The second line contains the start time $$$s$$$ in the described format HH:MM.

Output: For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.

Note: In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.   

Sample Inputs:
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04

Sample Outputs:
12:21
00:00
52:28
00:00
00:00

Solution (Python):
def func(num):
    if num == 2:
        return 5
    elif num == 5:
        return 2
    else:
        return num
for i in range(int(input())):
    (h1, m1) = map(str, input().split())
    h3 = '0' + h1
    m3 = '0' + m1
    (h3, h4, m3, m4) = (int(h3[:-1]), int(h3[-1]), int(m3[:-1]), int(m3[-1]))
    (h1, m1) = (int(h1), int(m1))
    s = input()
    (h2, m2) = (int(s[0:2]), int(s[3:]))
    s = [0, 1, 2, 5, 8]
    result = (0, 0)
    c = 0
    while h2 < h1:
        while h2 // 10 not in s and h2 < h1:
            h2 += 10 - h2 % 10
            m2 = 0
        while h2 % 10 not in s and h2 < h1:
            h2 += 1
            m2 = 0
            while h2 // 10 not in s and h2 < h1:
                h2 += 10 - h2 % 10
        if h2 >= h1:
            break
        while m2 < m1:
            while m2 // 10 not in s and m2 < m1:
                m2 += 10 - m2 % 10
            while m2 % 10 not in s and m2 < m1:
                m2 += 1
                while m2 // 10 not in s and m2 < m1:
                    m2 += 10 - m2 % 10
            if m2 >= m1:
                break
            if func(m2 % 10) * 10 + func(m2 // 10) < h1 and func(h2 % 10) * 10 + func(h2 // 10) < m1:
                result = (h2, m2)
                c = 1
                break
            m2 += 1
        m2 = 0
        h2 += 1
        if c != 0:
            break
    print('0' * (2 - len(str(result[0]))) + str(result[0]) + ':' + '0' * (2 - len(str(result[1]))) + str(result[1]))

Solution (Python):
correct = '01258'
bl = {'0': '1', '1': '2', '2': '5', '3': '5', '4': '5', '5': '8', '6': '8', '7': '8', '8': '0', '9': '0'}
inv = {'0': '0', '1': '1', '2': '5', '5': '2', '8': '8'}

def invert(time):
    t = ''
    for i in range(len(time)):
        t += inv[time[i]]
    return t

def corTime(time, cor1, cor2):
    n = len(time)
    f = False
    g = False
    for i in range(n):
        if time[i] not in correct:
            f = True
            if time[i] != '9':
                time = time[:i] + bl[time[i]] + '0' * (n - i - 1)
            else:
                time = time[:i - 1] + bl[time[i - 1]] + '0' * (n - i)
    if int(time) >= cor1:
        time = '0' * n
        g = True
    time2 = invert(time)
    if int(time2[::-1]) >= cor2:
        i = n - 1
        while True:
            if time[i] != '8':
                time = time[:i] + bl[time[i]] + '0' * (n - i - 1)
            else:
                if time[i - 1] == '8':
                    g = True
                time = time[:i - 1] + bl[time[i - 1]] + '0' * (n - i)
                i -= 1
            time2 = invert(time)
            if int(time) >= cor1:
                time = '0' * n
                g = True
                f = True
                break
            elif int(time2[::-1]) < cor2:
                f = True
                break
    return (time, f, g)
for _ in range(int(input())):
    (corh, corm) = map(int, input().split())
    (h, m) = input().split(':')
    (h, f, g) = corTime(h, corh, corm)
    if f:
        m = '0' * len(m)
    (m, f, g) = corTime(m, corm, corh)
    if g:
        h = h[:-1] + str(int(h[-1]) + 1)
        (h, f, g) = corTime(h, corh, corm)
        if f:
            m = '0' * len(m)
    print(h, m, sep=':')

Solution (Python):
ref = {'0': '0', '1': '1', '2': '5', '5': '2', '8': '8', ':': ':'}

def reflect(time):
    return ''.join([ref[time[i]] for i in range(len(time) - 1, -1, -1)])

def check(time):
    if any((num not in ref for num in time)):
        return False
    else:
        return True

def next(num):
    if num in ['2', '3', '4']:
        return '5'
    if num in ['5', '6', '7']:
        return '8'
    if num in ['8', '9']:
        return '0'
    return str(int(num) + 1)

def check_HH(HH, MM):
    while not check(HH) or int(reflect(HH)) >= m or int(HH) >= h:
        if not check(HH[0]):
            HH = next(HH[0]) + '0'
        else:
            HH = HH[0] + next(HH[1])
            if HH[1] == '0':
                HH = next(HH[0]) + HH[1]
        MM = '00'
    return (HH, MM)
cases = int(input())
for case in range(0, cases):
    inputs = [int(x) for x in input().split(' ')]
    time = input()
    HH = time[0:2]
    MM = time[3:]
    h = inputs[0]
    m = inputs[1]
    (HH, MM) = check_HH(HH, MM)
    while not check(MM) or int(reflect(MM)) >= h or int(MM) >= m:
        if not check(MM[0]):
            MM = next(MM[0]) + '0'
            if MM[0] == '0':
                HH = HH[0] + next(HH[1])
                if HH[1] == '0':
                    HH = next(HH[0]) + HH[1]
                (HH, MM) = check_HH(HH, MM)
        else:
            MM = MM[0] + next(MM[1])
            if MM[1] == '0':
                MM = next(MM[0]) + MM[1]
                if MM[0] == '0':
                    HH = HH[0] + next(HH[1])
                    if HH[1] == '0':
                        HH = next(HH[0]) + HH[1]
                    (HH, MM) = check_HH(HH, MM)
    print(HH + ':' + MM)