Skip to content

Instantly share code, notes, and snippets.

@reflechant
Last active September 14, 2018 20:17
Show Gist options
  • Save reflechant/e328ea4915ee1d3b8258c5c0cbeee4c7 to your computer and use it in GitHub Desktop.
Save reflechant/e328ea4915ee1d3b8258c5c0cbeee4c7 to your computer and use it in GitHub Desktop.
два будильника
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