-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.py
51 lines (39 loc) · 1.05 KB
/
1.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
DEBUG = 0
if DEBUG:
# N = [line.strip() for line in open('./in/1.test.txt').readlines()]
N = [line.strip() for line in open('./in/1.test.2.txt').readlines()]
else:
N = [line.strip() for line in open('./in/1.txt').readlines()]
def part_1():
return sum(
10 * (digits := [int(c) for c in ln if c.isnumeric()])[0] + digits[-1] for ln in N
)
def part_2():
digits = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
]
t = 0
for ln in N:
found = []
for i, c in enumerate(ln):
if c.isnumeric():
found.append(int(c))
continue
for value, digit in enumerate(digits, start=1):
if ln[i : i + len(digit)] == digit:
found.append(value)
t += 10 * found[0] + found[-1]
return t
if __name__ == '__main__':
print('--- Part 1 ---')
print(part_1())
print('\n--- Part 2 ---')
print(part_2())