From 874c91aa7cecc1403432b777e8bb58f34e308478 Mon Sep 17 00:00:00 2001 From: Timothy Quilling Date: Mon, 18 Dec 2023 11:01:30 -0500 Subject: [PATCH] Day 1 - Python --- src/day1/python/main.py | 39 ++++++++++++++++++++++++++++++++++++ src/day1/python/test_main.py | 28 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/day1/python/main.py create mode 100644 src/day1/python/test_main.py diff --git a/src/day1/python/main.py b/src/day1/python/main.py new file mode 100644 index 0000000..62e0d01 --- /dev/null +++ b/src/day1/python/main.py @@ -0,0 +1,39 @@ +def partA(input): + input = input.split("\n") + sum = 0 + for i in input: + print(i) + digits = (int(c) for c in i if c.isdigit()) + first_digit = next(digits) + last_digit = first_digit + while True: + try: + last_digit = next(digits) + except StopIteration: + break + combined = first_digit * 10 + last_digit + sum += combined + return sum + +def partB(input): + input = input.split("\n") + sum = 0 + digit_strings = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] + for i in input: + digit_array = [] + current_index = 0 + for c in i: + if c.isdigit(): + digit_array.append(int(c)) + elif c in "otfsen": + foresight = i[current_index:current_index+5] + for digit_string in digit_strings: + if foresight.startswith(digit_string): + digit_array.append(digit_strings.index(digit_string) + 1) + break + current_index += 1 + first_digit = digit_array[0] + last_digit = digit_array[-1] + combined = first_digit * 10 + last_digit + sum += combined + return sum diff --git a/src/day1/python/test_main.py b/src/day1/python/test_main.py new file mode 100644 index 0000000..2191b87 --- /dev/null +++ b/src/day1/python/test_main.py @@ -0,0 +1,28 @@ +from main import partA, partB + +day = 1 +answerExample = 142 +answerA = 55_029 +answerB = 55_686 +answerExampleB = 281 + +with open(f'src/day{day}/input.txt', 'r') as file: + input = file.read() + +with open(f'src/day{day}/input_example.txt', 'r') as file: + inputExample = file.read() + +with open(f'src/day{day}/input_example_b.txt', 'r') as file: + inputExampleB = file.read() + +def test_partA_example(): + assert partA(inputExample) == answerExample + +def test_partA(): + assert partA(input) == answerA + +def test_partB_example(): + assert partB(inputExampleB) == answerExampleB + +def test_partB(): + assert partB(input) == answerB \ No newline at end of file -- 2.51.2