From d5b8860595aeadac077c5c588d5b3cacba331507 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 23 Oct 2022 23:23:27 +1100 Subject: Simon's solution to challenge 187 --- challenge-187/sgreen/python/ch-1.py | 43 +++++++++++++++++++++++++++++++++++++ challenge-187/sgreen/python/ch-2.py | 28 ++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100755 challenge-187/sgreen/python/ch-1.py create mode 100755 challenge-187/sgreen/python/ch-2.py (limited to 'challenge-187/sgreen/python') 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]) diff --git a/challenge-187/sgreen/python/ch-2.py b/challenge-187/sgreen/python/ch-2.py new file mode 100755 index 0000000000..e76921c374 --- /dev/null +++ b/challenge-187/sgreen/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import sys +from decimal import Decimal +from itertools import combinations + + +def main(array): + solution = None + + # Turn the array into integers + n = [Decimal(x) for x in array] + + # Calculate for all combinations + for new in combinations(n, 3): + (a, b, c) = new + if a + b > c and b + c > a and a + c > b: + if solution is None or sum(new) > sum(solution): + solution = sorted(new, reverse = True) + + if solution: + print('(' + ', '.join(str(x) for x in solution) + ')') + else: + print('()') + + +if __name__ == '__main__': + main(sys.argv[1:]) -- cgit