Skip to content

Commit

Permalink
work on part 2, moved away from the regex replace solution to a syste…
Browse files Browse the repository at this point in the history
…m 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.
  • Loading branch information
aws-donco committed Dec 1, 2023
1 parent 0cf34e7 commit 8ced732
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
57 changes: 28 additions & 29 deletions day01.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
day01.part02(day01_input)

0 comments on commit 8ced732

Please sign in to comment.