diff options
| author | Simon Green <mail@simon.green> | 2025-11-23 23:05:41 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-11-23 23:05:41 +1000 |
| commit | 8361b08897b9065a52a34b4997cc98dbdfa2dd9c (patch) | |
| tree | bd49a8ef994445f597a3ceaaf3da7ce57afc89ca /challenge-348/sgreen/python/ch-2.py | |
| parent | 51f93bc0962522ed3bc5152f8266cc780c83f190 (diff) | |
| download | perlweeklychallenge-club-8361b08897b9065a52a34b4997cc98dbdfa2dd9c.tar.gz perlweeklychallenge-club-8361b08897b9065a52a34b4997cc98dbdfa2dd9c.tar.bz2 perlweeklychallenge-club-8361b08897b9065a52a34b4997cc98dbdfa2dd9c.zip | |
sgreen solutions to challenge 348
Diffstat (limited to 'challenge-348/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-348/sgreen/python/ch-2.py | 46 |
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() |
