-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1-2.py
35 lines (32 loc) · 873 Bytes
/
day1-2.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
#!/usr/bin/python3
nums = {
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
result = 0
with open("day1-1-input.txt") as f:
lines = f.readlines()
for line in lines:
found = []
for char in line:
if char.isnumeric():
found.append((char, line.index(char)))
for num in nums.keys():
index = line.find(num)
if index != -1:
found.append((nums[num], index))
last_index = line.rfind(num)
if last_index != -1 and (last_index != index):
found.append((nums[num], index + len(num)))
found.sort(key = lambda a: a[1])
first = found[0][0]
second = found[-1][0]
result += int(first + second)
print(result)