From 4751945dc8fdce408b03ddf78491aea0cf15cf81 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 17 Dec 2015 12:57:44 +0100 Subject: [PATCH] Solutions to day 17. --- day-17/input.txt | 20 ++++++++++++++++++++ day-17/solution.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 day-17/input.txt create mode 100644 day-17/solution.py diff --git a/day-17/input.txt b/day-17/input.txt new file mode 100644 index 0000000..6b25a72 --- /dev/null +++ b/day-17/input.txt @@ -0,0 +1,20 @@ +33 +14 +18 +20 +45 +35 +16 +35 +1 +13 +18 +13 +50 +44 +48 +6 +24 +41 +30 +42 diff --git a/day-17/solution.py b/day-17/solution.py new file mode 100644 index 0000000..cda8055 --- /dev/null +++ b/day-17/solution.py @@ -0,0 +1,37 @@ +from __future__ import print_function, division +import fileinput +from collections import defaultdict + +buckets = [] + +for line in fileinput.input(): + buckets.append(int(line)) + +def works(bucketCombination, target): + for idx, value in enumerate(buckets): + + if bucketCombination % 2 == 1: + target -= value + + bucketCombination = bucketCombination // 2 + + return target == 0 + +def ones(x): + n = 0 + while x > 0: + if x % 2: + n += 1 + + x //= 2 + + return n + +possible = defaultdict(lambda: 0) + +for x in range(1 << len(buckets)): + if works(x, 150): + n = ones(x) + possible[n] += 1 + +print(sum(possible[x] for x in possible), possible[min(possible.keys())])