AOC Day 1 puzzle solved
This commit is contained in:
1000
Day-1/Part 1/input.txt
Normal file
1000
Day-1/Part 1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
32
Day-1/Part 1/solution.py
Normal file
32
Day-1/Part 1/solution.py
Normal file
@ -0,0 +1,32 @@
|
||||
def findFirstDigitInString(string: str) -> str:
|
||||
length_of_string = len(string)
|
||||
i = 0
|
||||
while i < length_of_string :
|
||||
if ( string[i].isdigit()):
|
||||
return string[i]
|
||||
i+=1
|
||||
|
||||
numbers = []
|
||||
|
||||
with open("input.txt") as file:
|
||||
line = file.readline()
|
||||
while line:
|
||||
firstdigit =findFirstDigitInString(line)
|
||||
lastdigit = findFirstDigitInString(line[::-1]) # doing the same for the string in reverse will get the last digit
|
||||
|
||||
digit = firstdigit + lastdigit
|
||||
digit = int(digit)
|
||||
|
||||
print(f"found: {digit}")
|
||||
numbers.append(digit)
|
||||
|
||||
line = file.readline()
|
||||
|
||||
print(numbers)
|
||||
# now we'll need to sum them
|
||||
sum = 0
|
||||
for n in numbers:
|
||||
sum += n
|
||||
|
||||
|
||||
print(f"sum: {sum}")
|
Reference in New Issue
Block a user