From 5a84dcd95dd2a44980278bb9d008bad175dbc283 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Sat, 13 Feb 2021 20:14:10 +0100 Subject: [PATCH] Solved the pandigits, tried to do it way more complicated than necessary at first --- 32/main.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 32/main.py diff --git a/32/main.py b/32/main.py new file mode 100644 index 0000000..94e9cc7 --- /dev/null +++ b/32/main.py @@ -0,0 +1,53 @@ +# We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. +# The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital. +# Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. +# HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum. + +# So, we only need inputs where the summands are of size 5 and the output of size 4 + +from itertools import combinations +from itertools import permutations + +def splits(input, left, right): + assert len(input) == left + right + + result = [] + + combis = combinations(input, left) + for c in combis: + r = list(set(input) - set(c)) + result.append((list(c), r)) + + return result + +def check(series): + result = set() + + for i in range(4): + # print("Testing", toInt(series[0:i+1]) , toInt(series[i+1:5]) , toInt(series[5:])) + if toInt(series[0:i+1]) * toInt(series[i+1:5]) == toInt(series[5:]): + result.add(toInt(series[5:])) + + return result + +def toInt(l): + return int("".join(list(map(str,l)))) + +def main(): + print("Hello, this is Patrick") + + digits = range(1, 10) + + perms = permutations(digits) + + products = set() + for perm in perms: + products.update(check(perm)) + print(products) + + print(sum(products)) + + + +if __name__ == "__main__": + main() \ No newline at end of file