Let's see how far I get this year.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day01-1.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from helpers import Helper
  2. helper = Helper(debug=True)
  3. debug = helper.debug
  4. load_input = helper.load_input
  5. def main():
  6. from string import digits
  7. input_lines = load_input(1)
  8. # The calibration value of each line is the first and last digit
  9. # in each line, in order, combined to make a two-digit number.
  10. calibrations = []
  11. for line in input_lines:
  12. line = line.strip()
  13. if line == "":
  14. continue
  15. first = ""
  16. last = ""
  17. for i, char in enumerate(line):
  18. if char in digits:
  19. # print(f"Found first digit in {line} at position {i}: {char}")
  20. first = char
  21. break
  22. for i, char in enumerate(line[::-1]):
  23. if char in digits:
  24. # print(f"Found last digit in {line} at position {len(line) - i}: {char}")
  25. last = char
  26. break
  27. calibrations.append(int(first + last))
  28. print(calibrations)
  29. # The total calibration value is the sum of the individual
  30. # calibration values.
  31. print(f"Total calibration value: {sum(calibrations)}")
  32. if __name__ == "__main__":
  33. main()