From a001f39ff0bb4c671240ecc1e9d835ec7059fbc2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 22 May 2025 03:59:20 +0100 Subject: [PATCH] 2015: d24: ex1: add solution --- 2015/d24/ex1/ex1.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 2015/d24/ex1/ex1.py diff --git a/2015/d24/ex1/ex1.py b/2015/d24/ex1/ex1.py new file mode 100755 index 0000000..025bb30 --- /dev/null +++ b/2015/d24/ex1/ex1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +import itertools +import math +import sys + + +def solve(input: str) -> int: + def parse(input: str) -> list[int]: + return [int(n) for n in input.splitlines()] + + def package_groups(packages: list[int]) -> tuple[list[int], list[int], list[int]]: + assert sum(packages) % 3 == 0 # Sanity check + target_weight = sum(packages) // 3 + + # I'm lazy, a brute-force double loop is good enough + for first_len in range(1, len(packages) + 1): + for perm in itertools.combinations(range(len(packages)), first_len): + first = [packages[p] for p in perm] + if sum(first) != target_weight: + continue + others = [p for i, p in enumerate(packages) if i not in perm] + for sec_len in range(1, len(others) + 1): + for perm in itertools.combinations(range(len(others)), sec_len): + second = [others[p] for p in perm] + if sum(second) != target_weight: + continue + last = [p for i, p in enumerate(others) if i not in perm] + return first, second, last + assert False # Sanity check + + def quantum_entanglement(packages: list[int]) -> int: + return math.prod(packages) + + packages = parse(input) + best_split = package_groups(packages) + return quantum_entanglement(best_split[0]) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()