39 lines
865 B
Python
Executable file
39 lines
865 B
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
# https://adventofcode.com/2023/day/1
|
|
|
|
import re
|
|
|
|
|
|
def get_lines(filename: str) -> list:
|
|
with open(filename, "r") as file:
|
|
return [line.strip() for line in file.readlines()]
|
|
|
|
|
|
def get_cal_val(line: str) -> int:
|
|
found_first = False
|
|
first = ""
|
|
last = ""
|
|
for test_char in line:
|
|
if test_char.isdigit():
|
|
if found_first:
|
|
last = test_char
|
|
else:
|
|
first = test_char
|
|
found_first = True
|
|
if last == "":
|
|
last = first
|
|
return int(first + last)
|
|
|
|
|
|
def main():
|
|
# lines = get_lines("test-input.txt")
|
|
lines = get_lines("input.txt")
|
|
sum_cal_val = 0
|
|
for line in lines:
|
|
sum_cal_val += get_cal_val(line)
|
|
print(f"Part 1: The sum of all calibration values is {sum_cal_val}.")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|