AdventOfCode/2022/day4/day4_part1.py

54 lines
1.3 KiB
Python

import sys
from pprint import pprint
from functools import reduce
from operator import and_
with open(sys.argv[1],"r") as f:
line = [line.rstrip('\n').split(',') for line in f]
lines=[]
def contains(superset, subset) -> bool:
# creates a list of boolean values and
# combines them using the and operator
return reduce(and_, [i in superset for i in subset])
for i in line:
lines.append([i[0].split('-'),i[1].split('-')])
for i in range(0, len(lines)):
temp = []
if int(lines[i][0][0]) == int(lines[i][0][1]):
temp = [int(lines[i][0][0])]
else:
for j in range(int(lines[i][0][0]),int(lines[i][0][1])+1):
temp.append(j)
lines[i].append(temp)
temp = []
if int(lines[i][1][0]) == int(lines[i][1][1]):
temp = [int(lines[i][1][0])]
else:
for j in range(int(lines[i][1][0]),int(lines[i][1][1])+1):
temp.append(j)
lines[i].append(temp)
temp = []
superset = lines[i][2]
subset = lines[i][3]
if contains(superset,subset):
lines[i].append(True)
elif contains(subset,superset):
lines[i].append(True)
else:
lines[i].append(False)
count=0
for i in range(0,len(lines)):
if lines[i][4]:
count+=1
print(count)