|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import logging
- import re
-
- from functools import reduce
- from itertools import pairwise
- from sys import stdout
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- LOG_FILENAME = "./day03-1.log"
- INPUT_FILENAME = "./input03.txt"
-
- logger = logging.Logger(__name__)
- formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
- sh = logging.StreamHandler(stdout)
- sh.setLevel(logging.INFO)
- sh.setFormatter(formatter)
- fh = logging.FileHandler(LOG_FILENAME, mode="w", encoding="utf-8")
- fh.setLevel(logging.DEBUG)
- fh.setFormatter(formatter)
- logger.addHandler(sh)
- logger.addHandler(fh)
-
- def multiply_pair(s):
- pair = [int(x) for x in s[4:-1].split(",")]
- return reduce(lambda x,y: x*y, pair, 1)
-
- def main():
- with open(INPUT_FILENAME, "r") as f:
- line = "".join(f.readlines())
-
- MUL_REGEX = r"mul\(\d{1,3},\d{1,3}\)"
- matches = re.findall(MUL_REGEX, line)
- total_sum = reduce(lambda x,y: x+y, map(multiply_pair, matches))
- print(f"Total sum: {total_sum}")
-
-
- if __name__ == "__main__":
- main()
|