• 0 Posts
  • 1 Comment
Joined 11 months ago
cake
Cake day: December 1st, 2023

help-circle
  • Python 3

    I had some trouble getting Part 2 to work, until I realized that there could be overlap ( blbleightwoqsqs -> 82).

    spoiler
    import re
    
    def puzzle_one():
        result_sum = 0
        with open("inputs/day_01", "r", encoding="utf_8") as input_file:
            for line in input_file:
                number_list = [char for char in line if char.isnumeric()]
                number = int(number_list[0] + number_list[-1])
                result_sum += number
        return result_sum
    
    def puzzle_two():
        regex = r"(?=(zero|one|two|three|four|five|six|seven|eight|nine|[0-9]))"
        number_dict = {
            "zero": "0",
            "one": "1",
            "two": "2",
            "three": "3",
            "four": "4",
            "five": "5",
            "six": "6",
            "seven": "7",
            "eight": "8",
            "nine": "9",
        }
        result_sum = 0
        with open("inputs/day_01", "r", encoding="utf_8") as input_file:
            for line in input_file:
                number_list = [
                    number_dict[num] if num in number_dict else num
                    for num in re.findall(regex, line)
                ]
                number = int(number_list[0] + number_list[-1])
                result_sum += number
        return result_sum
    

    I still have a hard time understanding regex, but I think it’s getting there.