Skip to content

Commit

Permalink
run black reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire Fletcher committed Dec 1, 2023
1 parent 433cb9f commit 0ee9fc2
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 71 deletions.
81 changes: 49 additions & 32 deletions 2023/1/alternative.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,40 @@
import re

words_to_numbers = {
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9',
'zero': '0',
'eno': '1',
'owt': '2',
'eerht': '3',
'ruof': '4',
'evif': '5',
'xis': '6',
'neves': '7',
'thgie': '8',
'enin': '9',
'orez': '0'
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
"zero": "0",
"eno": "1",
"owt": "2",
"eerht": "3",
"ruof": "4",
"evif": "5",
"xis": "6",
"neves": "7",
"thgie": "8",
"enin": "9",
"orez": "0",
}


def read_file_into_matrix(file_name):
matrix = []
with open(file_name, 'r') as file:
with open(file_name, "r") as file:

for line in file:
row = list(line.strip())
matrix.append(row)

return matrix


## Find Digit - Part A
def find_digit(line):
for char in line:
Expand All @@ -43,6 +45,7 @@ def find_digit(line):

return None


## Find Digit - Part B
def find_digit_words(line):
concat = ""
Expand All @@ -57,41 +60,55 @@ def find_digit_words(line):
# Make sure to consider that it may not be an exact match if it isn't the start or end
concat += char
if any(k in concat for k in words_to_numbers):
return [k for key, k in words_to_numbers.items() if key in concat][0] # List comprehension
return [k for key, k in words_to_numbers.items() if key in concat][
0
] # List comprehension

return None


def decode_calibration_value(first_digit, last_digit):
return int(first_digit + last_digit)


def calculate_summed_calibration_value(calibration_values):
sum = 0
for i in calibration_values:
sum = sum + i

return sum


def main():
print("Reading input file...")
matrix = read_file_into_matrix("/home/claire/claire-fletcher/advent-of-code/2023/1/input.txt")
matrix = read_file_into_matrix(
"/home/claire/claire-fletcher/advent-of-code/2023/1/input.txt"
)

print("Calulating first and last digits...")
calibration_values_a, calibration_values_b = [], []
for line in matrix:
backwards = list(reversed(line))

calibration_values_a.append(
decode_calibration_value(find_digit(line),
find_digit(backwards)))
decode_calibration_value(find_digit(line), find_digit(backwards))
)

calibration_values_b.append(
decode_calibration_value(find_digit_words(line),
find_digit_words(backwards)))
decode_calibration_value(
find_digit_words(line), find_digit_words(backwards)
)
)

print(
"Final Result Part A: ",
calculate_summed_calibration_value(calibration_values_a),
)
print(
"Final Result Part B: ",
calculate_summed_calibration_value(calibration_values_b),
)

print("Final Result Part A: ",
calculate_summed_calibration_value(calibration_values_a))
print("Final Result Part B: ",
calculate_summed_calibration_value(calibration_values_b))

if __name__ == "__main__":
main()
main()
14 changes: 9 additions & 5 deletions 2023/1/graveyard.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Cool things I didn't use in the end


def find_all_digits(line):
"""
Returns a generator that filters the digits from the given line.
Expand All @@ -21,10 +22,13 @@ def find_last_digit(line):
if char.isdigit():
return char
else:
# Concatenate the string until a number matches the regex
# Concatenate the string until a number matches the regex
concat += char
number = re.findall(r'(?=(orez|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin)\b)\w+',
concat, flags=re.IGNORECASE)
number = re.findall(
r"(?=(orez|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin)\b)\w+",
concat,
flags=re.IGNORECASE,
)
if number.__len__() == 1:
return words_to_numbers[number[0][::-1]] # Reverse the string
return None
return words_to_numbers[number[0][::-1]] # Reverse the string
return None
59 changes: 37 additions & 22 deletions 2023/1/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@
import re

words_to_numbers = {
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9',
'zero': '0',
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
"zero": "0",
}


def read_file_into_matrix(file_name):
matrix = []
with open(file_name, 'r') as file:
with open(file_name, "r") as file:

for line in file:
row = list(line.strip())
matrix.append(row)

return matrix


## Find Digit - Part A
def find_digit(line):
for char in line:
Expand All @@ -33,17 +35,20 @@ def find_digit(line):

return None


## Find Digit - Part B
def match_num_pattern(concat):
pattern = r'(?=(zero|one|two|three|four|five|six|seven|eight|nine)\b)\w+'
pattern = r"(?=(zero|one|two|three|four|five|six|seven|eight|nine)\b)\w+"

return re.findall(pattern, concat, flags=re.IGNORECASE)


def match_num_pattern_reversed(concat):
pattern = r'(?=(orez|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin)\b)\w+'
pattern = r"(?=(orez|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin)\b)\w+"

return re.findall(pattern, concat, flags=re.IGNORECASE)


def find_digit_words(line, reversed):
concat = ""

Expand Down Expand Up @@ -71,16 +76,19 @@ def find_digit_words(line, reversed):

return None


def decode_calibration_value(first_digit, last_digit):
return int(first_digit + last_digit)


def calculate_summed_calibration_value(calibration_values):
sum = 0
for i in calibration_values:
sum = sum + i

return sum


def main():
print("Reading input file...")
matrix = read_file_into_matrix("input.txt")
Expand All @@ -91,17 +99,24 @@ def main():
backwards = list(reversed(line))

calibration_values_a.append(
decode_calibration_value(find_digit(line),
find_digit(backwards)))
decode_calibration_value(find_digit(line), find_digit(backwards))
)

calibration_values_b.append(
decode_calibration_value(find_digit_words(line, False),
find_digit_words(backwards, True)))
decode_calibration_value(
find_digit_words(line, False), find_digit_words(backwards, True)
)
)

print(
"Final Result Part A: ",
calculate_summed_calibration_value(calibration_values_a),
)
print(
"Final Result Part B: ",
calculate_summed_calibration_value(calibration_values_b),
)

print("Final Result Part A: ",
calculate_summed_calibration_value(calibration_values_a))
print("Final Result Part B: ",
calculate_summed_calibration_value(calibration_values_b))

if __name__ == "__main__":
main()
main()
16 changes: 10 additions & 6 deletions 2023/1/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Using PyTest
from main import *


def test_part1():
matrix = read_file_into_matrix("test-input-a.txt")

Expand All @@ -9,10 +10,11 @@ def test_part1():
backwards = list(reversed(line))

calibration_values_a.append(
decode_calibration_value(find_digit(line),
find_digit(backwards)))
decode_calibration_value(find_digit(line), find_digit(backwards))
)

assert calculate_summed_calibration_value(calibration_values_a) == 142

assert calculate_summed_calibration_value(calibration_values_a) == 142

def test_part2():
matrix = read_file_into_matrix("test-input-b.txt")
Expand All @@ -22,7 +24,9 @@ def test_part2():
backwards = list(reversed(line))

calibration_values_b.append(
decode_calibration_value(find_digit_words(line, False),
find_digit_words(backwards, True)))
decode_calibration_value(
find_digit_words(line, False), find_digit_words(backwards, True)
)
)

assert calculate_summed_calibration_value(calibration_values_b) == 281
assert calculate_summed_calibration_value(calibration_values_b) == 281
16 changes: 10 additions & 6 deletions 2023/1/tests/test_main_dict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Using PyTest
from alternative import *


def test_part1():
matrix = read_file_into_matrix("test-input-a.txt")

Expand All @@ -9,10 +10,11 @@ def test_part1():
backwards = list(reversed(line))

calibration_values_a.append(
decode_calibration_value(find_digit(line),
find_digit(backwards)))
decode_calibration_value(find_digit(line), find_digit(backwards))
)

assert calculate_summed_calibration_value(calibration_values_a) == 142

assert calculate_summed_calibration_value(calibration_values_a) == 142

def test_part2():
matrix = read_file_into_matrix("test-input-b.txt")
Expand All @@ -22,7 +24,9 @@ def test_part2():
backwards = list(reversed(line))

calibration_values_b.append(
decode_calibration_value(find_digit_words(line),
find_digit_words(backwards)))
decode_calibration_value(
find_digit_words(line), find_digit_words(backwards)
)
)

assert calculate_summed_calibration_value(calibration_values_b) == 281
assert calculate_summed_calibration_value(calibration_values_b) == 281

0 comments on commit 0ee9fc2

Please sign in to comment.