From 641aa833290fb4ba76db692aa7346e6b136e8e34 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 2 Dec 2021 09:48:12 +0100 Subject: Take functional python to the next level --- 2015/17/puzzle-1.py | 19 ++++++++++++++----- 2015/17/puzzle-2.py | 29 ++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) (limited to '2015/17') diff --git a/2015/17/puzzle-1.py b/2015/17/puzzle-1.py index b201e5b..184d8dc 100755 --- a/2015/17/puzzle-1.py +++ b/2015/17/puzzle-1.py @@ -7,11 +7,20 @@ def main() -> None: with open("input", "r", encoding="utf-8") as f: nums = list(map(int, f.readlines())) - combs: list[tuple[int, ...]] = [] - for n in range(len(nums) + 1): - combs += list(itertools.combinations(nums, n)) - - print(len(list(filter(lambda x: sum(x) == 150, combs)))) + print( + len( + list( + filter( + lambda x: sum(x) == 150, + list( + itertools.chain( + *[list(itertools.combinations(nums, n)) for n in range(len(nums) + 1)] + ) + ), + ) + ) + ) + ) if __name__ == "__main__": diff --git a/2015/17/puzzle-2.py b/2015/17/puzzle-2.py index 43f8922..50a70f9 100755 --- a/2015/17/puzzle-2.py +++ b/2015/17/puzzle-2.py @@ -7,13 +7,28 @@ def main() -> None: with open("input", "r", encoding="utf-8") as f: nums = list(map(int, f.readlines())) - combs: list[tuple[int, ...]] = [] - for n in range(len(nums) + 1): - combs += list(itertools.combinations(nums, n)) - - combs = list(filter(lambda x: sum(x) == 150, combs)) - conts = min(map(len, combs)) - print(len(list(filter(lambda x: len(x) == conts, combs)))) + print( + len( + list( + filter( + lambda x: len(x) == min(map(len, combs)), + ( + combs := list( + filter( + lambda x: sum(x) == 150, + itertools.chain( + *[ + list(itertools.combinations(nums, n)) + for n in range(len(nums) + 1) + ] + ), + ) + ) + ), + ) + ) + ) + ) if __name__ == "__main__": -- cgit v1.2.3