Day01 solved

This commit is contained in:
WieErWill 2023-12-01 17:28:32 +01:00
commit cd21bb2e76
4 changed files with 1109 additions and 0 deletions

1000
Day01/coordinates.txt Normal file

File diff suppressed because it is too large Load Diff

50
Day01/part1.py Normal file
View File

@ -0,0 +1,50 @@
def extract_calibration_value(line):
"""
Extract the calibration value from a line of text.
The calibration value is formed by combining the first digit and the last digit in the line.
:param line: A string representing a line of text from the document.
:return: The calibration value as an integer.
"""
first_digit = None
last_digit = None
# Find the first digit in the line
for char in line:
if char.isdigit():
first_digit = char
break
# Find the last digit in the line
for char in reversed(line):
if char.isdigit():
last_digit = char
break
# Combine the first and last digits to form the calibration value
if first_digit and last_digit:
return int(first_digit + last_digit)
else:
return 0
def sum_calibration_values(filename):
"""
Calculate the sum of all calibration values in the document.
:param filename: The name of the file containing the document.
:return: The sum of all calibration values.
"""
total = 0
# Open the file and process each line
with open(filename, 'r') as file:
for line in file:
total += extract_calibration_value(line)
return total
# Main execution
if __name__ == "__main__":
filename = "coordinates.txt"
total_calibration_value = sum_calibration_values(filename)
print(f"Total Sum of Calibration Values: {total_calibration_value}")

52
Day01/part2.py Normal file
View File

@ -0,0 +1,52 @@
def extract_digits(line):
"""Extracts all digits (as numbers) from the line in the order they appear."""
digit_map = {
"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4",
"five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9"
}
# Add single digit mappings
digit_map.update({str(k): str(k) for k in range(10)})
digits_found = []
i = 0
while i < len(line):
matched = False
for word, digit in digit_map.items():
if line.startswith(word, i):
digits_found.append(int(digit))
i += len(word) - 1 # Advance the index
matched = True
break
i += 1 # Move to the next character if no match
return digits_found
def extract_calibration_value(line):
"""Extracts the calibration value from a line."""
digits = extract_digits(line)
if digits:
return int(str(digits[0]) + str(digits[-1]))
return 0
def sum_calibration_values(file_path):
"""Extracts calibration values from each line and sums them up."""
total_sum = 0
with open(file_path, 'r') as file:
for line in file:
calibration_value = extract_calibration_value(line.strip())
if calibration_value:
# Use the first and last found digit to form the calibration value
total_sum += calibration_value
print(f"Line: {line.strip()} - Calibration Value: {calibration_value}")
else:
print(f"Line: {line.strip()} - No digits found")
return total_sum
# Main execution
if __name__ == "__main__":
import sys
filename = sys.argv[1] if len(sys.argv) > 1 else "coordinates.txt"
total_calibration_value = sum_calibration_values(filename)
print(f"Total Sum of Calibration Values: {total_calibration_value}")

7
Day01/test.txt Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen