Completed day 4
This commit is contained in:
23
2022/Day-04/day04-1.py
Normal file
23
2022/Day-04/day04-1.py
Normal file
@@ -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)
|
23
2022/Day-04/day04-2.py
Normal file
23
2022/Day-04/day04-2.py
Normal file
@@ -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)
|
Reference in New Issue
Block a user