[Advent of Code 2024](https://adventofcode.com/2024)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import logging
  2. import re
  3. from functools import reduce
  4. from itertools import pairwise
  5. from sys import stdout
  6. # It seems like the goal of the program is just to multiply some numbers.
  7. # It does that with instructions like mul(X,Y), where X and Y are each 1-3
  8. # digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a
  9. # result of 2024. Similarly, mul(123,4) would multiply 123 by 4.
  10. # However, because the program's memory has been corrupted, there are also
  11. # many invalid characters that should be ignored, even if they look like
  12. # part of a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), or
  13. # mul ( 2 , 4 ) do nothing.
  14. # For example, consider the following section of corrupted memory:
  15. # xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
  16. # Adding up the result of each real instruction produces 161
  17. # (2*4 + 5*5 + 11*8 + 8*5).
  18. # There are two new instructions you'll need to handle:
  19. # The do() instruction enables future mul instructions.
  20. # The don't() instruction disables future mul instructions.
  21. # Only the most recent do() or don't() instruction applies.
  22. # At the beginning of the program, mul instructions are enabled.
  23. LOG_FILENAME = "./day03-1.log"
  24. INPUT_FILENAME = "./input03.txt"
  25. logger = logging.Logger(__name__)
  26. formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
  27. sh = logging.StreamHandler(stdout)
  28. sh.setLevel(logging.INFO)
  29. sh.setFormatter(formatter)
  30. fh = logging.FileHandler(LOG_FILENAME, mode="w", encoding="utf-8")
  31. fh.setLevel(logging.DEBUG)
  32. fh.setFormatter(formatter)
  33. logger.addHandler(sh)
  34. logger.addHandler(fh)
  35. def multiply_pair(s):
  36. pair = [int(x) for x in s[4:-1].split(",")]
  37. return reduce(lambda x,y: x*y, pair, 1)
  38. def main():
  39. with open(INPUT_FILENAME, "r") as f:
  40. line = "".join(f.readlines())
  41. MUL_REGEX = r"((do|don't)\(\)|mul\(\d{1,3},\d{1,3}\))"
  42. matches = re.findall(MUL_REGEX, line)
  43. ops = [x[0] for x in matches]
  44. logger.info(ops[:25])
  45. good_ops = []
  46. toggle = True
  47. for o in ops:
  48. if not toggle and o == "do()":
  49. toggle = True
  50. continue
  51. if toggle:
  52. if o == "don't()":
  53. toggle = False
  54. continue
  55. if "mul" in o:
  56. good_ops.append(o)
  57. total_sum = reduce(lambda x,y: x+y, map(multiply_pair, good_ops))
  58. logger.info(f"Total sum: {total_sum}")
  59. if __name__ == "__main__":
  60. main()