# Advent of Code 2025 https://adventofcode.com/2025 Why do I make these separate repos every year?
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

day2-1.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # encoding: utf-8
  2. # Given a list of comma-separated pairs of numbers separated by a dash
  3. # e.g. 111-999,22-33
  4. # find all the numbers that are some sequence of digits repeated twice
  5. import logging
  6. logger = logging.Logger(__name__)
  7. logger.addHandler(logging.StreamHandler())
  8. logger.setLevel(logging.DEBUG)
  9. def main():
  10. with open("day2_input.txt", "r") as f:
  11. input_line = f.readline().strip()
  12. inranges = [x.strip() for x in input_line.split(",")]
  13. num_ranges = len(inranges)
  14. tenths = num_ranges//10
  15. logger.debug(f"Received {num_ranges} ranges.")
  16. all_invalids = []
  17. for i, r in enumerate(inranges):
  18. if tenths and i % tenths == 0:
  19. logger.debug(f"Processing range {i}.")
  20. x, y = [int(q) for q in r.strip().split("-")]
  21. invalids = []
  22. for k in range(x,y+1):
  23. s_k = str(k)
  24. l = len(s_k)
  25. m = l//2
  26. if l % 2 == 0:
  27. if s_k[:m] == s_k[m:]:
  28. logger.debug(f"Found a double: {k}")
  29. invalids.append(k)
  30. all_invalids.extend(invalids)
  31. logger.info(f"Found {len(all_invalids)} invalid IDs.")
  32. logger.info(f"Sum of invalid IDs is {sum(all_invalids)}.")
  33. if __name__ == "__main__":
  34. main()