Advent of Code 2022 https://adventofcode.com/2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day4-2.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from string import ascii_letters
  2. DEBUG=False
  3. def debug(*args):
  4. if DEBUG:
  5. print(args)
  6. def within(first, second):
  7. if ((first[0] <= second[0] and first[1] >= second[1])
  8. or (second[0] <= first[0] and second[1] >= first[1])):
  9. return True
  10. return False
  11. def overlap(first, second):
  12. if ((first[0] < second[0] and first[1] < second[0])
  13. or (first[0] > second[1] and first[1] > second[1])
  14. or (second[0] < first[0] and second[1] < first[0])
  15. or (second[0] > first[1] and second[1] > first[1])):
  16. return False
  17. return True
  18. def main():
  19. with open("input4.txt", "r") as file:
  20. inlines = [line.strip() for line in file.readlines()]
  21. overlaps = []
  22. range_pairs = []
  23. for line in inlines:
  24. first, second = line.split(",")
  25. sfirst = first.split("-")
  26. pfirst = (int(sfirst[0]), int(sfirst[1]))
  27. ssecond = second.split("-")
  28. psecond = (int(ssecond[0]), int(ssecond[1]))
  29. if overlap(pfirst, psecond):
  30. overlaps.append(1)
  31. else:
  32. overlaps.append(0)
  33. range_pairs.append((pfirst, psecond))
  34. print(sum(overlaps))
  35. if __name__ == "__main__":
  36. main()