AdventOfCode/2022/day4/day4_part2.py

53 lines
1.2 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(A, B):
n = len(A)
return any(A == B[i:i + n] for i in range(len(B)-n + 1))
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 any(item in superset for item in subset):
lines[i].append(True)
elif any(item in subset for item in 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)