Add 2023-1
This commit is contained in:
commit
131b44139d
2 changed files with 1040 additions and 0 deletions
40
1/main.py
Executable file
40
1/main.py
Executable file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
digit_map = {
|
||||
'zero': 0,
|
||||
'one': 1,
|
||||
'two': 2,
|
||||
'three': 3,
|
||||
'four': 4,
|
||||
'five': 5,
|
||||
'six': 6,
|
||||
'seven': 7,
|
||||
'eight': 8,
|
||||
'nine': 9
|
||||
}
|
||||
|
||||
def find_digit_name(line):
|
||||
for digit_name in digit_map.keys():
|
||||
if line.startswith(digit_name):
|
||||
return digit_map[digit_name]
|
||||
return None
|
||||
|
||||
def find_digit(line, check_range):
|
||||
for i in check_range:
|
||||
if line[i].isdigit():
|
||||
return int(line[i])
|
||||
if (digit := find_digit_name(line[i:])) is not None:
|
||||
return digit
|
||||
|
||||
|
||||
# read input file
|
||||
with open('input', 'r') as f:
|
||||
lines = f.readlines()
|
||||
lines_sum = 0
|
||||
for line in lines:
|
||||
first_digit = find_digit(line, range(len(line)))
|
||||
last_digit = find_digit(line, range(len(line) - 1, -1, -1))
|
||||
num = str(first_digit) + str(last_digit)
|
||||
lines_sum += int(num)
|
||||
|
||||
print(lines_sum)
|
Loading…
Reference in a new issue