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-1.py 917B

123456789101112131415161718192021222324252627282930313233343536373839
  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 main():
  12. with open("input4.txt", "r") as file:
  13. inlines = [line.strip() for line in file.readlines()]
  14. overlaps = []
  15. range_pairs = []
  16. for line in inlines:
  17. first, second = line.split(",")
  18. sfirst = first.split("-")
  19. pfirst = (int(sfirst[0]), int(sfirst[1]))
  20. ssecond = second.split("-")
  21. psecond = (int(ssecond[0]), int(ssecond[1]))
  22. if within(pfirst, psecond):
  23. overlaps.append(1)
  24. else:
  25. overlaps.append(0)
  26. range_pairs.append((pfirst, psecond))
  27. print(sum(overlaps))
  28. if __name__ == "__main__":
  29. main()