From 89d7a33abb8f1f26a7887dda72621a2abd0262ef Mon Sep 17 00:00:00 2001 From: Ben Maurer Date: Wed, 7 Dec 2022 11:41:57 -0600 Subject: [PATCH] There's definitely a better answer to this. --- day_4.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 day_4.py diff --git a/day_4.py b/day_4.py new file mode 100644 index 0000000..85f9545 --- /dev/null +++ b/day_4.py @@ -0,0 +1,36 @@ +import aocd +from dotenv import load_dotenv + +load_dotenv() +puzzle_input = aocd.get_data(day=4, year=2022) + +pairs = puzzle_input.split('\n') +pair_number = 0 +contained = 0 +overlap = 0 + +for pair in pairs: + pair = list(map(int, pair.replace(',','-').split('-'))) + # Contained + if pair[0] <= pair[2] <= pair[3] <= pair[1]: + contained = contained + 1 + elif pair[2] <= pair[0] <= pair[1] <= pair[3]: + contained = contained + 1 + else: + pass + # Overlapping + if pair[0] <= pair[2] <= pair[1]: + overlap = overlap + 1 + elif pair[0] <= pair[3] <= pair[1]: + overlap = overlap + 1 + elif pair[2] <= pair[0] <= pair[3]: + overlap = overlap + 1 + elif pair[2] <= pair[1] <= pair[3]: + overlap = overlap + 1 + else: + pass + +print(f'{contained} of the elves\' tasks are contained by others.') +print(f'{overlap} of the tasks overlap.') + +