-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_part1.py
41 lines (28 loc) · 1.13 KB
/
01_part1.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
import sys
from functools import reduce
"""
Advent of Code - Day 1: Trebuchet?!
Get the input from https://adventofcode.com/2023/day/1/input, save it to a file and feed it to stdin
ex. `python 01_part1.py < day_one_input.txt`
"""
def get_test_data():
"""Get test data from stdin"""
return [line.rstrip() for line in sys.stdin]
get_first_digit_in_string = lambda x: next(d for d in x if d.isnumeric())
get_last_digit_in_string = lambda x: get_first_digit_in_string(reversed(x))
sum_numbers = lambda x, y: x + y
def extract_calibration_value_from_string(input: str) -> int:
"""Extract first and last digit from given string"""
try:
digits = get_first_digit_in_string(input) + get_last_digit_in_string(input)
return int(digits)
except StopIteration:
# no digits in given input string
return 0
def main() -> int:
data = get_test_data()
# extract calibration value from each item in data, and sum all of them together
result = reduce(sum_numbers, [extract_calibration_value_from_string(line) for line in data], 0)
print(result)
if __name__ == "__main__":
sys.exit(main())