From e36ab6b9ff714005ece1e13595546827293e1033 Mon Sep 17 00:00:00 2001 From: Fabio Gallotti Date: Sat, 6 Dec 2025 22:43:57 +0100 Subject: [PATCH] feat: 2025 day06 --- README.md | 3 +- src/year2025/day06/exercises.py | 10 +++++ src/year2025/day06/functions.py | 77 +++++++++++++++++++++++++++++++++ tests/2025/test_2025day06.py | 16 +++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/year2025/day06/exercises.py create mode 100644 src/year2025/day06/functions.py create mode 100644 tests/2025/test_2025day06.py diff --git a/README.md b/README.md index 62de94c..0ba4b1b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ | 03 | [✓][032015] | | | | [✓][032019] | [✓][032020] | [✓][032021] | [✓][032022] | [✓][032023] | [✓][032024] | [✓][032025] | | 04 | [✓][042015] | | | | [✓][042019] | [✓][042020] | [✓][042021] | [✓][042022] | [✓][042023] | [✓][042024] | [✓][042025] | | 05 | [✓][052015] | | | | | [✓][052020] | | [✓][052022] | [✓][052023] | [✓][052024] | [✓][052025] | -| 06 | [✓][062015] | | | | | [✓][062020] | | [✓][062022] | [✓][062023] | [✓][062024] | | +| 06 | [✓][062015] | | | | | [✓][062020] | | [✓][062022] | [✓][062023] | [✓][062024] | [✓][062025] | | 07 | [✓][072015] | | | | | [✓][072020] | | [✓][072022] | [✓][072023] | [✓][072024] | | | 08 | [✓][082015] | | | | | [✓][082020] | | [✓][082022] | [✓][082023] | [✓][082024] | | | 09 | [✓][092015] | | | | | [✓][092020] | | [✓][092022] | [✓][092023] | [✓][092024] | | @@ -134,6 +134,7 @@ [032025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day03 [042025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day04 [052025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day05 +[062025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day06 ## To run an exercise ## diff --git a/src/year2025/day06/exercises.py b/src/year2025/day06/exercises.py new file mode 100644 index 0000000..3aadbf0 --- /dev/null +++ b/src/year2025/day06/exercises.py @@ -0,0 +1,10 @@ +from functions.read_input import read_input +from inputs.path import PATH + +from .functions import part_1, part_2 + +data = read_input(f"{PATH}/2025/day06.txt") + +print(f"First part: {part_1(data)}") + +print(f"Second part: {part_2(data)}") diff --git a/src/year2025/day06/functions.py b/src/year2025/day06/functions.py new file mode 100644 index 0000000..8256e3b --- /dev/null +++ b/src/year2025/day06/functions.py @@ -0,0 +1,77 @@ +def preprocessing(data): + list_numbers = [] + operations = [elem for elem in data[-1].split(" ") if elem] + + for elem in data[:-1]: + numbers = elem.split(" ") + list_numbers.append([int(num) for num in numbers if num]) + + vertical_list = list(zip(*list_numbers)) + + return vertical_list, operations + + +def part_1(data): + list_numbers, operations = preprocessing(data) + list_total = [] + for i in range(len(list_numbers)): + if operations[i] == "+": + list_total.append(sum(list_numbers[i])) + elif operations[i] == "*": + product = 1 + for num in list_numbers[i]: + product *= num + list_total.append(product) + + return sum(list_total) + + +def preprocessing_part2(data): + list_numbers = [] + operations_with_whitespaces = [elem for elem in data[-1].split(" ")] + operations = [elem for elem in data[-1].split(" ") if elem] + i = [i for i, x in enumerate(operations_with_whitespaces) if x] + lengths = [j - k for k, j in zip(i, [*i[1:], len(operations_with_whitespaces)])] + + for elem in data[:-1]: + numbers = elem.split(" ") + + for i in range(len(numbers)): + if numbers[i] == "": + numbers[i] = "0" + + numbers = [n for num in numbers for n in num] + + for i in range(len(numbers)): + if numbers[i] == "0": + numbers[i] = "" + + list_numbers.append(numbers) + + vertical_list = list(zip(*list_numbers)) + vertical_list_of_list = [ + vertical_list[sum(lengths[:i]) : sum(lengths[: i + 1])] for i in range(len(lengths)) + ] + + list_numbers = [ + [int("".join(x for x in elem if x != "")) for elem in list_ if any(x != "" for x in elem)] + for list_ in vertical_list_of_list + ] + + return list_numbers, operations + + +def part_2(data): + list_numbers, operations = preprocessing_part2(data) + + list_total = [] + for i in range(len(list_numbers)): + if operations[i] == "+": + list_total.append(sum(list_numbers[i])) + elif operations[i] == "*": + product = 1 + for num in list_numbers[i]: + product *= num + list_total.append(product) + + return sum(list_total) diff --git a/tests/2025/test_2025day06.py b/tests/2025/test_2025day06.py new file mode 100644 index 0000000..51017eb --- /dev/null +++ b/tests/2025/test_2025day06.py @@ -0,0 +1,16 @@ +from year2025.day06.functions import part_1, part_2 + +EXAMPLE_1 = [ + "123 328 51 64 ", + " 45 64 387 23 ", + " 6 98 215 314", + "* + * + ", +] + + +def test_part_1(): + assert part_1(EXAMPLE_1) == 4277556 + + +def test_part_2(): + assert part_2(EXAMPLE_1) == 3263827