Last active
November 28, 2024 21:46
-
-
Save franckalbinet/cec9784528496a9849e30e80755d2398 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Final solution | |
| lut = {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9'} | |
| def get_digits(line): | |
| pattern = r'(?=(\d|one|two|three|four|five|six|seven|eight|nine))' | |
| matches = re.finditer(pattern, line) | |
| result = [] | |
| for match in matches: | |
| digit = match.group(1) | |
| if match.group(1) in lut: | |
| result.append(lut[digit]) | |
| else: | |
| result.append(digit) | |
| return ''.join(result) | |
| def get_calibration_value(line): | |
| digits = get_digits(line) | |
| return int(digits[0]+ digits[-1]) | |
| def solve_day1(inp): | |
| result = 0 | |
| for o in inp.splitlines(): | |
| result += get_calibration_value(o) | |
| return result | |
| if __name__ == "__main__": | |
| from aocd import get_data | |
| inp = get_data(day=1, year=2023) | |
| print(solve_day1(inp)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment