9.py 803 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. from itertools import combinations
  2. lines = []
  3. with open("9.input") as f:
  4. for line in f.readlines():
  5. lines.append(int(line.strip()))
  6. queue, rest = lines[0:25], lines[25:]
  7. invalid_num = None
  8. while rest != []:
  9. next = rest.pop(0)
  10. valid = next in [a + b for (a, b) in combinations(list(set(queue)), 2)]
  11. if not valid:
  12. invalid_num = next
  13. break
  14. queue.pop(0)
  15. queue.append(next)
  16. print("Answer 1: " + str(invalid_num))
  17. def search_window(size):
  18. for i in range(0, len(lines)):
  19. slice = lines[i:i+size]
  20. if sum(slice) == invalid_num:
  21. return min(slice) + max(slice)
  22. return None
  23. for size in range(2, len(lines)):
  24. result = search_window(size)
  25. if result != None:
  26. print("Answer 2: " + str(result))
  27. break