Skip to content

Commit

Permalink
feat(01/2023): add solutions and tests day 01/2023
Browse files Browse the repository at this point in the history
  • Loading branch information
aleattene committed Oct 16, 2024
1 parent 352d16a commit b6d054c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
File renamed without changes.
Empty file.
File renamed without changes.
30 changes: 30 additions & 0 deletions year_2023/day_01_trebuchet/solution_one_day_01_2023.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def solve_day_01_2023_one(filename: str) -> int | str:
calibration_sum = 0
try:
# Open the file and read the data using the utf-8 encoding
with open(filename, 'r', encoding='utf-8') as f:
# Iterate over each line in the data and extract the numbers
for line in f:
first_digit = None
last_digit = None

# We call the enumerate function only once for each line, not for each character
enumerated_line = enumerate(line)
for i, char in enumerated_line:

# Found first digit
if first_digit is None and char.isdigit():
first_digit = char

# Found last digit
if last_digit is None and line[-(i + 1)].isdigit():
last_digit = line[-(i + 1)]

# If we found the first and last digit, we can break the loop
if first_digit and last_digit:
calibration_sum += int(first_digit + last_digit)
break

return calibration_sum
except Exception as error:
return "Error: {}".format(str(error).replace('\n', ''))
35 changes: 35 additions & 0 deletions year_2023/day_01_trebuchet/solution_two_day_01_2023.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import re


def solve_day_01_2023_two(filename: str) -> int | str:
verbose_to_numbers = {
'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9,
}
try:
calibration_sum = 0
# Open the file and read the data using the utf-8 encoding
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
matches = []

# Found all the matches for the digits and add to the matches list
for match in re.finditer(r'\d', line):
matches.append((match.start(), match.group()))

# Found all the matches for the verbose numbers and add to the matches list
for key, value in verbose_to_numbers.items():
for match in re.finditer(key, line):
matches.append((match.start(), str(value)))
print(matches)
# Sort the matches list by the first element of each tuple (using a lambda/anonymous function)
matches.sort(key=lambda elem: elem[0])
# First digit is the first element of the first tuple
first_digit = matches[0][1]
# Last digit is the second element of the last tuple
last_digit = matches[-1][1]

calibration_sum += int(first_digit + last_digit)

return calibration_sum
except Exception as error:
return "Error: {}".format(str(error).replace('\n', ''))
18 changes: 18 additions & 0 deletions year_2023/day_01_trebuchet/test_day_01_2023.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from .solution_one_day_01_2023 import solve_day_01_2023_one
from .solution_two_day_01_2023 import solve_day_01_2023_two

filename = "input.txt"
current_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_dir, filename)


def test_first_part():
result = solve_day_01_2023_one(file_path)
assert result == 56397


def test_second_part():
result = solve_day_01_2023_two(file_path)
assert result == 55701

0 comments on commit b6d054c

Please sign in to comment.