This commit is contained in:
Sijmen 2023-12-01 14:15:03 +01:00
commit bd1cf6ffa0
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

52
day1.py Normal file
View file

@ -0,0 +1,52 @@
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)