Skip to content

Commit

Permalink
Initial push v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Donald-rdex committed Dec 1, 2023
0 parents commit 5bf95a3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
46 changes: 46 additions & 0 deletions day01.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5bf95a3

Please sign in to comment.