|
| 1 | +# This draft is not the solution (it does not work) |
| 2 | +with open("day1_input.txt", "r") as input_file: |
| 3 | + input_string = input_file.read() |
| 4 | + |
| 5 | +input_lines = input_string.splitlines() |
| 6 | + |
| 7 | +number_words_dict = { |
| 8 | + "one": "1", |
| 9 | + "two": "2", |
| 10 | + "three": "3", |
| 11 | + "four": "4", |
| 12 | + "five": "5", |
| 13 | + "six": "6", |
| 14 | + "seven": "7", |
| 15 | + "eight": "8", |
| 16 | + "nine": "9" |
| 17 | +} |
| 18 | + |
| 19 | +calibration_values_sum = 0 |
| 20 | + |
| 21 | +for line_idx, line in enumerate(input_lines): |
| 22 | + first_idx = None |
| 23 | + first_digit = None |
| 24 | + last_idx = None |
| 25 | + last_digit = None |
| 26 | + for number_word in number_words_dict.keys(): |
| 27 | + number_word_idx = line.find(number_word) |
| 28 | + if number_word_idx != -1: |
| 29 | + if first_idx is None: |
| 30 | + first_idx = number_word_idx |
| 31 | + first_digit = number_words_dict[number_word] |
| 32 | + elif number_word_idx < first_idx: |
| 33 | + if last_idx is None: |
| 34 | + last_idx = first_idx |
| 35 | + last_digit = first_digit |
| 36 | + first_idx = number_word_idx |
| 37 | + first_digit = number_words_dict[number_word] |
| 38 | + elif last_idx is None: |
| 39 | + last_idx = number_word_idx |
| 40 | + last_digit = number_words_dict[number_word] |
| 41 | + elif number_word_idx > last_idx: |
| 42 | + last_idx = number_word_idx |
| 43 | + last_digit = number_words_dict[number_word] |
| 44 | + for char_idx, char in enumerate(line): |
| 45 | + if char.isdigit(): |
| 46 | + if first_idx is None: |
| 47 | + first_idx = char_idx |
| 48 | + first_digit = char |
| 49 | + elif char_idx < first_idx: |
| 50 | + first_idx = char_idx |
| 51 | + first_digit = char |
| 52 | + elif last_idx is None: |
| 53 | + last_idx = char_idx |
| 54 | + last_digit = char |
| 55 | + elif char_idx > last_idx: |
| 56 | + last_idx = char_idx |
| 57 | + last_digit = char |
| 58 | + |
| 59 | + if last_digit is None: |
| 60 | + last_digit = first_digit |
| 61 | + calibration_value = int(first_digit + last_digit) |
| 62 | + calibration_values_sum += calibration_value |
| 63 | + |
| 64 | + if line_idx == 20: |
| 65 | + print("line " + str(line_idx + 1) + ": " + line) |
| 66 | + print("first_digit:", first_digit, "idx:", first_idx) |
| 67 | + print("last_digit:", last_digit, "idx:", last_idx) |
| 68 | + print("resulting calibration value:", calibration_value) |
| 69 | + print() |
| 70 | + |
| 71 | + |
| 72 | +print("Sum of calibration values:", calibration_values_sum) |
0 commit comments