Last active
September 14, 2018 20:17
-
-
Save reflechant/e328ea4915ee1d3b8258c5c0cbeee4c7 to your computer and use it in GitHub Desktop.
два будильника
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import lru_cache | |
def timestr_to_tuple(s): | |
return tuple(int(x) for x in s.split(':')) | |
@lru_cache(maxsize=None) | |
def count(time, newtime): | |
x = min(abs(newtime[0] - time[0]), abs(newtime[0] + time[0] - 24)) + min( | |
abs(newtime[1] - time[1]), abs(newtime[1] + time[1] - 24)) | |
return x | |
def walk(time1, time2, times): | |
if times: | |
return min( | |
count(time1, times[0]) + walk(times[0], time2, times[1:]), | |
count(time2, times[0]) + walk(time1, times[0], times[1:])) | |
else: | |
return 0 | |
def main(): | |
time1_str, time2_str = input().split() | |
time1 = timestr_to_tuple(time1_str) | |
time2 = timestr_to_tuple(time2_str) | |
n = int(input()) | |
times = tuple(timestr_to_tuple(input()) for i in range(n)) | |
print(walk(time1, time2, times)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment