solve day 01 part 1
This commit is contained in:
parent
e268519cc5
commit
d270ffbf1a
2 changed files with 32 additions and 2 deletions
|
|
@ -2,14 +2,37 @@
|
|||
|
||||
# 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")
|
||||
# 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__':
|
||||
|
|
|
|||
|
|
@ -8,6 +8,13 @@ class TestThing(unittest.TestCase):
|
|||
def setUp(self):
|
||||
pass
|
||||
|
||||
def testGetCalVal(self):
|
||||
self.assertEqual(program.get_cal_val("a2c3"), 23)
|
||||
self.assertEqual(program.get_cal_val("1c3d"), 13)
|
||||
self.assertEqual(program.get_cal_val("3a2c3er"), 33)
|
||||
self.assertEqual(program.get_cal_val("1234567"), 17)
|
||||
self.assertEqual(program.get_cal_val("asdw3wemr"), 33)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue