[Advent of Code 2024](https://adventofcode.com/2024)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. from itertools import pairwise
  3. from sys import stdout
  4. logger = logging.Logger(__name__)
  5. formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
  6. sh = logging.StreamHandler(stdout)
  7. sh.setLevel(logging.INFO)
  8. sh.setFormatter(formatter)
  9. fh = logging.FileHandler("./day02-1.log", mode="w", encoding="utf-8")
  10. fh.setLevel(logging.DEBUG)
  11. fh.setFormatter(formatter)
  12. logger.addHandler(sh)
  13. logger.addHandler(fh)
  14. # So, a report only counts as safe if both of the following are true:
  15. # The levels are either all increasing or all decreasing.
  16. # Any two adjacent levels differ by at least one and at most three.
  17. def main():
  18. with open("input02.txt", "r", encoding="utf-8") as f:
  19. lines = [list(map(int,l.split(" "))) for l in f.readlines()]
  20. line_stats = []
  21. for i, line in enumerate(lines):
  22. pw_l = list(pairwise(line))
  23. ascending = all([x < y for x,y in pw_l])
  24. descending = all([x > y for x,y in pw_l])
  25. lessthan = all([abs(x - y) <= 3 for x,y in pw_l])
  26. safe = (ascending or descending) and lessthan
  27. line_dict = {"ascending": ascending, "descending": descending, "less than 3": lessthan, "safe": (ascending or descending) and lessthan}
  28. line_stats.append(line_dict)
  29. if i % 5 == 0:
  30. logger.info(f"Spot check: {line} is {'' if safe else 'UN'}safe")
  31. else:
  32. logger.debug(f"{line}: {line_dict}")
  33. print(f"Number of safe lines: {sum([1 for s in line_stats if s['safe']])}")
  34. if __name__ == "__main__":
  35. main()