1234567891011121314151617181920212223242526272829303132333435 |
- def main():
- from string import digits
-
- with open("day01.input", "r") as file:
- input_lines = file.readlines()
-
- # The calibration value of each line is the first and last digit
- # in each line, in order, combined to make a two-digit number.
- calibrations = []
- for line in input_lines:
- line = line.strip()
- if line == "":
- continue
- first = ""
- last = ""
- for i, char in enumerate(line):
- if char in digits:
- # print(f"Found first digit in {line} at position {i}: {char}")
- first = char
- break
- for i, char in enumerate(line[::-1]):
- if char in digits:
- # print(f"Found last digit in {line} at position {len(line) - i}: {char}")
- last = char
- break
- calibrations.append(int(first + last))
-
- print(calibrations)
-
- # The total calibration value is the sum of the individual
- # calibration values.
- print(f"Total calibration value: {sum(calibrations)}")
-
- if __name__ == "__main__":
- main()
|