116 lines
3.7 KiB
Python
116 lines
3.7 KiB
Python
# import os
|
|
# import re
|
|
|
|
# cwd = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
# # file_name = 'advent2023_p1.txt'
|
|
# file_name = 'test.txt'
|
|
# advent = os.path.join(cwd, file_name)
|
|
|
|
# nums = []
|
|
# values = []
|
|
|
|
# # test = "eighthree"
|
|
|
|
# # numbers = {
|
|
# # 'one': 1, 'two': 2, 'three': 3, 'four': 4,
|
|
# # 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9
|
|
# # }
|
|
# numbers_to_string = {
|
|
# 'one': '1', 'two': '2', 'three': '3', 'four': '4',
|
|
# 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9'
|
|
# }
|
|
|
|
|
|
|
|
# with open(advent, 'r') as file:
|
|
# for line in file: # Read lines
|
|
# # Split the line when a digit has been found
|
|
# digit_index = re.findall(r'(?:(one|two|three|four|five|six|seven|eight|nine)|(\d))', line)
|
|
# # print(digit_index)
|
|
# if digit_index:
|
|
# # Use the tuple method to find if the value is present
|
|
# for num,num2 in digit_index:
|
|
# # Always the spelled out number, convert to string digit
|
|
# if num:
|
|
# # if num in numbers:
|
|
# # nums.append()
|
|
# nums.append(numbers_to_string[num])
|
|
# # Pass the string digit
|
|
# if num2:
|
|
# nums.append(num2)
|
|
# print(nums)
|
|
# # values.append()
|
|
# if len(nums) < 2:
|
|
# num_one = nums[0]
|
|
# num_two = num_one
|
|
# num_join = num_one + num_two
|
|
# values.append(int(num_join))
|
|
# else:
|
|
# num_one = nums[0]
|
|
# num_two = nums[-1]
|
|
# num_join = num_one + num_two
|
|
# values.append(int(num_join))
|
|
# nums.clear()
|
|
# # print(values)
|
|
# # print(sum(values))
|
|
# # print(numbers_as_integers)
|
|
|
|
# file.close()
|
|
|
|
import re
|
|
import os
|
|
|
|
cwd = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
# file_name = 'advent2023_p1.txt'
|
|
file_name = 'test.txt'
|
|
advent = os.path.join(cwd, file_name)
|
|
|
|
def extract_numbers(text):
|
|
# Regular expression to match digits and number words
|
|
num_pattern = r'(?:zero|one|two|three|four|five|six|seven|eight|nine|\d)+'
|
|
|
|
# Find all occurrences of numbers in the text
|
|
numbers = re.findall(num_pattern, text)
|
|
|
|
# Function to convert number words to digits
|
|
def words_to_digits(word):
|
|
words_to_digits_dict = {
|
|
'zero': '0', 'one': '1', 'two': '2', 'three': '3',
|
|
'four': '4', 'five': '5', 'six': '6', 'seven': '7',
|
|
'eight': '8', 'nine': '9'
|
|
}
|
|
return ''.join(words_to_digits_dict.get(w, w) for w in re.findall(r'\w+', word))
|
|
|
|
# Convert all number words to digits
|
|
numbers = [words_to_digits(num) for num in numbers]
|
|
|
|
# Find the first and last numbers in the list
|
|
first_number = next((num for num in numbers if any(c.isdigit() for c in num)), None)
|
|
last_number = next((num for num in reversed(numbers) if any(c.isdigit() for c in num)), None)
|
|
|
|
return first_number, last_number
|
|
|
|
# Read text from the file
|
|
with open(advent, 'r') as file:
|
|
lines = file.readlines()
|
|
|
|
# Initialize total sum
|
|
total_sum = 0
|
|
|
|
# Process each line in the file
|
|
for line in lines:
|
|
# Extract first and last numbers from the line
|
|
first_num, last_num = extract_numbers(line)
|
|
|
|
# Calculate the sum of individual digits from the first and last numbers
|
|
if first_num is not None and last_num is not None:
|
|
first_sum = sum(int(digit) for digit in str(first_num))
|
|
last_sum = sum(int(digit) for digit in str(last_num))
|
|
line_sum = first_sum + last_sum
|
|
total_sum += line_sum
|
|
|
|
# Print the total sum of individual digits from the first and last numbers in all lines
|
|
print("Total sum of individual digits from first and last numbers in all lines:", total_sum)
|