From 1209bb95c0d8808158f82e9fb4b51bb12d000e30 Mon Sep 17 00:00:00 2001 From: Jaime9070 Date: Sat, 2 Dec 2023 11:14:37 +0100 Subject: [PATCH] dia 1 en python --- .gitignore | 1 + python/Day 1/calibration_value.py | 36 +++++++++++++++++++++++++ python/Day 1/calibration_value_part2.py | 36 +++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 python/Day 1/calibration_value.py create mode 100644 python/Day 1/calibration_value_part2.py diff --git a/.gitignore b/.gitignore index dae6469..de76faa 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ **/debug/ **/target/ **/*rs.bk +**/calibration_document.txt \ No newline at end of file diff --git a/python/Day 1/calibration_value.py b/python/Day 1/calibration_value.py new file mode 100644 index 0000000..adf464e --- /dev/null +++ b/python/Day 1/calibration_value.py @@ -0,0 +1,36 @@ +#Import the module that give us access to work with files. Allows us to manage the file-related input. +from io import open + +# We open the txt document containing the calibration code +calibration_document=open('calibration_document.txt','r') + +# We read the text inside distinguishing by lines +text=calibration_document.readlines() + +# Close the txt file to be able to work on the text without having the file open. +calibration_document.close() +del(calibration_document) + +# Define the digits that we want to search in the text +numbers = ['0','1','2','3','4','5','6','7','8','9'] +calibration_value=0 +list_numbers=[] + +# Combining the first digit and the last digit to form a single two-digit number to obtein the calibration values +for line in text: + code='' + for letter in line: + if letter in numbers: + code=code+letter + break + for letter in line[::-1]: + if letter in numbers: + code=code+letter + break + list_numbers.append(int(code)) + +# The sum of all of the calibration values is the solution of the problem to to repare the trebuchet +calibration_values=sum(list_numbers) + +#print(list_numbers) +print('The calibration value is {}'.format(calibration_values)) \ No newline at end of file diff --git a/python/Day 1/calibration_value_part2.py b/python/Day 1/calibration_value_part2.py new file mode 100644 index 0000000..c368836 --- /dev/null +++ b/python/Day 1/calibration_value_part2.py @@ -0,0 +1,36 @@ +#Import the module that give us access to work with files. Allows us to manage the file-related input. +from io import open + +# We open the txt document containing the calibration code +calibration_document=open('calibration document.txt','r') + +# We read the text inside distinguishing by lines +text=calibration_document.readlines() + +# Close the txt file to be able to work on the text without having the file open. +calibration_document.close() +del(calibration_document) + +# Define the digits that we want to search in the text +numbers = ['0','1','2','3','4','5','6','7','8','9'] +calibration_value=0 +list_numbers=[] + +# Combining the first digit and the last digit to form a single two-digit number to obtein the calibration values +for line in text: + code='' + for letter in line: + if letter in numbers: + code=code+letter + break + for letter in line[::-1]: + if letter in numbers: + code=code+letter + break + list_numbers.append(int(code)) + +# The sum of all of the calibration values is the solution of the problem to to repare the trebuchet +calibration_values=sum(list_numbers) + +#print(list_numbers) +print('The calibration value is {}'.format(calibration_values)) \ No newline at end of file