From e8858136abd4f987626c930ef8d01cd052021fef Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 4 Dec 2022 12:44:36 +0000 Subject: [PATCH] Completed day 4 --- 2022/Day-04/day04-1.py | 23 +++++++++++++++++++++++ 2022/Day-04/day04-2.py | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 2022/Day-04/day04-1.py create mode 100644 2022/Day-04/day04-2.py diff --git a/2022/Day-04/day04-1.py b/2022/Day-04/day04-1.py new file mode 100644 index 0000000..4cf3531 --- /dev/null +++ b/2022/Day-04/day04-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +fullOverlaps = 0 + +# For each line in the input file, construct a list of all sections to be cleaned. +with open('input.txt', 'r') as inputFile: + for line in inputFile: + instructions = line.strip().split(',') + startStop = [] + for group in instructions: + sublist = [] + sublist.append(int(group.split('-')[0])) + sublist.append(int(group.split('-')[1])) + startStop.append(sublist) + + groups = [list(range(startStop[0][0], startStop[0][1] + 1)), list(range(startStop[1][0], startStop[1][1] + 1))] + + # Check if the lists are sublists of each other. + if(set(groups[0]) <= set(groups[1]) or set(groups[1]) <= set(groups[0])): + fullOverlaps += 1 + +# Print the answer. +print(fullOverlaps) diff --git a/2022/Day-04/day04-2.py b/2022/Day-04/day04-2.py new file mode 100644 index 0000000..499bfdb --- /dev/null +++ b/2022/Day-04/day04-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +overlaps = 0 + +# For each line in the input file, construct a list of all sections to be cleaned. +with open('input.txt', 'r') as inputFile: + for line in inputFile: + instructions = line.strip().split(',') + startStop = [] + for group in instructions: + sublist = [] + sublist.append(int(group.split('-')[0])) + sublist.append(int(group.split('-')[1])) + startStop.append(sublist) + + groups = [list(range(startStop[0][0], startStop[0][1] + 1)), list(range(startStop[1][0], startStop[1][1] + 1))] + + # Check if the lists share any elements. + if(len(set(groups[0]).intersection(set(groups[1]))) > 0): + overlaps += 1 + +# Print the answer. +print(overlaps)