programming puzzle solutions
1def partA(input):
2 input = input.split("\n")
3 sum = 0
4 for i in input:
5 digits = (int(c) for c in i if c.isdigit())
6 first_digit = next(digits)
7 last_digit = first_digit
8 while True:
9 try:
10 last_digit = next(digits)
11 except StopIteration:
12 break
13 combined = first_digit * 10 + last_digit
14 sum += combined
15 return sum
16
17def partB(input):
18 input = input.split("\n")
19 sum = 0
20 digit_strings = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
21 for i in input:
22 digit_array = []
23 current_index = 0
24 for c in i:
25 if c.isdigit():
26 digit_array.append(int(c))
27 elif c in "otfsen":
28 foresight = i[current_index:current_index+5]
29 for digit_string in digit_strings:
30 if foresight.startswith(digit_string):
31 digit_array.append(digit_strings.index(digit_string) + 1)
32 break
33 current_index += 1
34 first_digit = digit_array[0]
35 last_digit = digit_array[-1]
36 combined = first_digit * 10 + last_digit
37 sum += combined
38 return sum