Advertisement
icarussiano

Day4 AoC2022 Python3

Dec 4th, 2022 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. Part 1
  2. somma=0
  3. for line in open('input.txt'):
  4.     a=line.strip().split(',')
  5.     x= list(map(int,a[0].split('-')))
  6.     y = list(map(int, a[1].split('-')))
  7.     #possibile anche farlo con i set e usare x.issubset(y) or y.issubset(x)
  8.     if all( item in list(range(x[0],x[1]+1)) for item in list(range(y[0],y[1]+1))) or all(item in list(range(y[0],y[1]+1)) for item in list(range(x[0],x[1]+1))) :
  9.         somma+=1
  10. print(somma)
  11.  
  12. Part 1 one-line
  13. print(len(list(filter(lambda x: int(x[0][0])<=int(x[1][0]) and int(x[0][1])>=int(x[1][1]) or (int(x[1][0])<=int(x[0][0]) and int(x[1][1])>=int(x[0][1])),[list(map(lambda x: x.split('-'),l.strip().split(','))) for l in open('input.txt')]))))
  14.  
  15. Part 2
  16. somma=0
  17. for line in open('input.txt'):
  18.     a=line.strip().split(',')
  19.     x= list(map(int,a[0].split('-')))
  20.     y = list(map(int, a[1].split('-')))
  21.     #possibile anche farlo come in Part 1 ma con any al posto di all
  22.     if set(range(x[0],x[1]+1)).intersection(set(range(y[0],y[1]+1))):
  23.         somma+=1
  24. print(somma)
  25.  
  26. Part 2 one-line
  27. print(len(list(filter(lambda x: int(x[1][0])<=int(x[0][1]) and int(x[1][0])>=int(x[0][0]) or (int(x[0][0])<=int(x[1][1])  and int(x[0][0])>=int(x[1][0])),[list(map(lambda x: x.split('-'),l.strip().split(','))) for l in open('input.txt')]))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement