-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(01/2023): add solutions and tests day 01/2023
- Loading branch information
Showing
6 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|