From 8ced732420b8e1e763c3e7f534c4feb7003df126 Mon Sep 17 00:00:00 2001 From: Donald Cowart Date: Fri, 1 Dec 2023 14:48:42 +0000 Subject: [PATCH] work on part 2, moved away from the regex replace solution to a system that parsed the string moving in a window along the string makes for time complexity of len(inputline) * number of regex matches. single line added to main.py for PEP8 formatting. --- day01.py | 57 ++++++++++++++++++++++++++++---------------------------- main.py | 2 +- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/day01.py b/day01.py index 446d487..fbf5b0e 100644 --- a/day01.py +++ b/day01.py @@ -6,43 +6,42 @@ def part01(day01_input_lines: list) -> None: regex = re.compile(r'\D') total_sum = 0 for line in day01_input_lines: - numline = regex.sub('', line) - newNum = numline[0] + numline[-1] - total_sum += int(newNum) + num_line = regex.sub('', line) + new_num = num_line[0] + num_line[-1] + total_sum += int(new_num) print(total_sum) def part02(day01_input_lines): - # not yet working - regex = re.compile(r'\D') - number_replacement_re = [(re.compile(r'nine'), '9'), - (re.compile(r'eight'), '8'), - (re.compile(r'seven'), '7'), - (re.compile(r'six'), '6'), - (re.compile(r'five'), '5'), - (re.compile(r'four'), '4'), - (re.compile(r'three'), '3'), - (re.compile(r'two'), '2'), - (re.compile(r'one'), '1'), + # after a lot of stop/start... working + digit_re = re.compile(r'\d') + non_digit_re = re.compile(r'\D') + number_replacement_re = [('nine', re.compile(r'nine'), '9'), + ('eight', re.compile(r'eight'), '8'), + ('seven', re.compile(r'seven'), '7'), + ('six', re.compile(r'six'), '6'), + ('five', re.compile(r'five'), '5'), + ('four', re.compile(r'four'), '4'), + ('three', re.compile(r'three'), '3'), + ('two', re.compile(r'two'), '2'), + ('one', re.compile(r'one'), '1'), ] total_sum = 0 for line in day01_input_lines: new_line = line - re_find_order = {} - - for (num_line_re, replacement) in number_replacement_re: - if num_line_re.search(new_line) is not None: - re_find_order[num_line_re.search(new_line).start()] = replacement - re_sorted_find_order = sorted(re_find_order) - replacements = [pair for pair in number_replacement_re if pair[1] in re_sorted_find_order] - for (pair_re, replacement) in replacements: - new_line = pair_re.sub(replacement, new_line) - print(new_line) + found_number = '' + index = 0 + for possible_digit in new_line: + if digit_re.match(possible_digit): + found_number += possible_digit + else: + for number_tup in number_replacement_re: + if number_tup[1].match(new_line[index:]): + found_number += number_tup[2] + index += 1 - print(new_line) - new_line = regex.sub('', line) - newNum = new_line[0] + new_line[-1] - print(newNum) - total_sum += int(newNum) + found_number = non_digit_re.sub('', found_number) + number_pair = found_number[0] + found_number[-1] + total_sum += int(number_pair) print(total_sum) diff --git a/main.py b/main.py index fbaa399..81aa497 100644 --- a/main.py +++ b/main.py @@ -7,4 +7,4 @@ day01_input = day01fp.readlines() day01_input = [f'{i}'.strip() for i in day01_input] day01.part01(day01_input) - day01.part02(day01_input) \ No newline at end of file + day01.part02(day01_input)