From 5bf95a3cdf8c117d63f74875b6cf5f9395c3b016 Mon Sep 17 00:00:00 2001 From: Donald Date: Fri, 1 Dec 2023 08:14:31 +0000 Subject: [PATCH] Initial push v2 --- day01.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ main.py | 10 ++++++++++ 2 files changed, 56 insertions(+) create mode 100644 day01.py create mode 100644 main.py diff --git a/day01.py b/day01.py new file mode 100644 index 0000000..3125be3 --- /dev/null +++ b/day01.py @@ -0,0 +1,46 @@ +import re + + +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) + print(total_sum) + + +def part02(day01_input_lines): + 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'), + ] + 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) + + print(new_line) + new_line = regex.sub('', line) + newNum = new_line[0] + new_line[-1] + print(newNum) + total_sum += int(newNum) + print(total_sum) diff --git a/main.py b/main.py new file mode 100644 index 0000000..fbaa399 --- /dev/null +++ b/main.py @@ -0,0 +1,10 @@ +import day01 + +if __name__ == '__main__': + + day01_input_file = 'problem_inputs/day01_input.txt' + with open(day01_input_file) as day01fp: + 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