-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
49 lines (44 loc) · 1.33 KB
/
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
import re;
def calculate() -> int:
with open("input.txt") as file:
lines = [line.rstrip() for line in file]
sum = 0
for line in lines:
allDigits = []
for i, c in enumerate(line):
if c.isdigit():
allDigits.append(c)
print(allDigits)
code = allDigits[0] + allDigits[-1]
sum = sum + int(code)
return sum
def calculate2() -> int:
digits = {
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine"
}
with open("input.txt") as file:
lines = [line.rstrip() for line in file]
sum = 0
for line in lines:
allDigits = dict()
for num, let in digits.items():
indices = [m.start() for m in re.finditer(let, line)]
for index in indices:
if index != -1:
allDigits[index] = num
for index, char in enumerate(line):
if char.isdigit():
allDigits[index] = char
code = allDigits[min(allDigits)] + allDigits[max(allDigits)]
print(code)
sum = sum + int(code)
print(sum)
return sum