-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
51 lines (39 loc) · 905 Bytes
/
day1.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
43
44
45
46
47
48
49
50
51
import re
test_input = '''two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen'''
test_input = test_input.split('\n')
numbers = {
"one":1,
"two":2,
"three":3,
"four":4,
"five":5,
"six":6,
"seven":7,
"eight":8,
"nine":9
}
with open("day1.txt") as f:
data = f.read().split('\n')
def day1task2(data):
no_numbers = []
for line in data:
no_numbers.append(re.findall("(?=([1-9]|one|two|three|four|five|six|seven|eight|nine))",line))
total = 0
for line in no_numbers:
number = ''
index_we_want = [0,-1]
for i in index_we_want:
if line[i].isdigit():
number+=line[i]
else:
number+=str(numbers[line[i]])
total+=int(number)
return total
print(day1task2(test_input))
print(day1task2(data))