My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2023/01

+40
+40
2023/day01.py
··· 1 + import fileinput 2 + 3 + DIGITS = {str(i): i for i in range(1, 10)} 4 + 5 + DIGIT_WORDS = { 6 + 'one': 1, 7 + 'two': 2, 8 + 'three': 3, 9 + 'four': 4, 10 + 'five': 5, 11 + 'six': 6, 12 + 'seven': 7, 13 + 'eight': 8, 14 + 'nine': 9, 15 + } 16 + 17 + 18 + def calibration(line, dictionary): 19 + start = None 20 + end = None 21 + for i, c in enumerate(line): 22 + for key, val in dictionary.items(): 23 + if line[i:].startswith(key): 24 + if start is None: 25 + start = val 26 + end = val 27 + 28 + return start * 10 + end 29 + 30 + 31 + part_1 = 0 32 + part_2 = 0 33 + 34 + for line in fileinput.input(): 35 + part_1 += calibration(line, DIGITS) 36 + part_2 += calibration(line, DIGITS | DIGIT_WORDS) 37 + 38 + print("Part 1:", part_1) 39 + print("Part 2:", part_2) 40 +