diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2022-10-24 17:44:04 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2022-10-24 17:44:04 +0800 |
| commit | 7783ecb7dad2ae5ea3d3022a13e3901c196c4154 (patch) | |
| tree | fa408d0b19c541602d81799c8a13eea61df2cb8f /challenge-187/sgreen/python/ch-1.py | |
| parent | a406c6935f88c7afa04a707a2611ebe4fe4eadae (diff) | |
| parent | bb06570f6b1634ca14bfd08927f3bfc6c052c494 (diff) | |
| download | perlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.tar.gz perlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.tar.bz2 perlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-187/sgreen/python/ch-1.py')
| -rwxr-xr-x | challenge-187/sgreen/python/ch-1.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-187/sgreen/python/ch-1.py b/challenge-187/sgreen/python/ch-1.py new file mode 100755 index 0000000000..5aa9ac45b7 --- /dev/null +++ b/challenge-187/sgreen/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import sys +from datetime import date + + +def get_dofy(dt): + '''Turn a date in DD-MM format into day of year''' + (day, month) = dt.split('-') + d = date(2022, int(month), int(day)) + return int(d.strftime('%j')) + + +def main(array): + '''Calculate the days that foo and bar spent together''' + + # Convert the input into the day of year + foo_start = get_dofy(array[0]) + foo_end = get_dofy(array[1]) + bar_start = get_dofy(array[2]) + bar_end = get_dofy(array[3]) + + # Make sure they arrive before they leave! + if foo_end < foo_start: + raise ValueError('foo left before they arrived!') + if bar_end < bar_start: + raise ValueError('bar left before they arrived!') + + # The time they spent together is when the last person arrives and the first one leave. + both_start = max(foo_start, bar_start) + both_end = min(foo_end, bar_end) + + # Display the days spent together + if both_end < both_start: + print('0 days') + elif both_start == both_end: + print('1 day') + else: + print(f'{both_end - both_start + 1} days') + + +if __name__ == '__main__': + main(sys.argv[1:5]) |
