2015: d24: ex1: add solution
This commit is contained in:
parent
e8c323465f
commit
a001f39ff0
1 changed files with 46 additions and 0 deletions
46
2015/d24/ex1/ex1.py
Executable file
46
2015/d24/ex1/ex1.py
Executable file
|
|
@ -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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue