aboutsummaryrefslogtreecommitdiff
path: root/challenge-348/sgreen/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-348/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-348/sgreen/python/ch-2.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-348/sgreen/python/ch-2.py b/challenge-348/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..8c92aae94e
--- /dev/null
+++ b/challenge-348/sgreen/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+def time_to_minute(s: str) -> int:
+ if not re.match(r'^\d{1,2}:\d{2}$', s):
+ raise ValueError("Invalid time format, should be HH:MM")
+
+ hour, minute = map(int, s.split(':'))
+
+ if hour < 0 or hour > 23 or minute < 0 or minute > 59:
+ raise ValueError("Invalid time format")
+
+ return hour * 60 + minute
+
+
+def convert_time(source: str, target: str) -> int:
+ # Convert the time to minutes past midnight, and calculate the minutes
+ # between them
+ duration = time_to_minute(target) - time_to_minute(source)
+ if duration < 0:
+ # Adjust for next day
+ duration += 24 * 60
+
+ # Possible increments in minutes
+ moves = [60, 15, 5, 1]
+ count = 0
+
+ for move in moves:
+ # Use as many of this move as possible
+ count += duration // move
+
+ # The remaining minutes
+ duration %= move
+
+ return count
+
+
+def main():
+ result = convert_time(sys.argv[1], sys.argv[2])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()