2023/day1.py

53 lines
813 B
Python
Raw Normal View History

2023-12-01 13:15:03 +00:00
import fileinput
digit_map = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
}
digits = list(digit_map)
inp = list(fileinput.input())
#
# Part 1
#
part1 = sum(
[
int(
next(x for x in y if x.isdigit())
+ next(x for x in reversed(y) if x.isdigit())
)
for y in inp
]
)
print(part1)
#
# Part 2
#
part2 = 0
for line in inp:
newline = []
for i in range(len(line)):
for digit in digits:
if line[i:].startswith(digit):
newline.append(str(digit_map[digit]))
break
else:
if line[i].isdigit():
newline.append(line[i])
part2 += int(newline[0] + newline[-1])
print(part2)