-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1-challenge.py
45 lines (30 loc) · 991 Bytes
/
day1-challenge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# Created by David Clark on 12/06/2023
#
#
import re
with open("input.txt", "r") as f:
lines = f.readlines()
new_lines = ' '.join(lines).replace('\n','').split()
# for part 2, TBD
numChars = ['one','two','three','four','five','six','seven','eight','nine']
def find_first_last_occurrences(lines):
results = []
for line in new_lines:
# Find all occurrences of numbers in the string
numbers = re.findall(r'\d', line)
print(numbers)
if numbers:
# Extract the first and last occurrences of a number
first_occurrence = (numbers[0])
last_occurrence = (numbers[-1])
results.append(first_occurrence + last_occurrence)
else:
# Handle the case where no numbers are found in the string
results.append(None)
return results
occurrences = find_first_last_occurrences(lines)
sum = 0
for occurrence in occurrences:
sum = sum + int(occurrence)
print(sum)