From 7efb4fb78f6765307066b7647ae3aef4920155d7 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Wed, 21 Apr 2021 21:53:35 -0400 Subject: [PATCH] Solve problem 90 --- python/e090.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 python/e090.py diff --git a/python/e090.py b/python/e090.py new file mode 100644 index 0000000..87c33fb --- /dev/null +++ b/python/e090.py @@ -0,0 +1,64 @@ +# +def choose(xs, n): + """ + Computes r choose n, in other words choose n from xs. + Returns combinations and not permutations. + """ + + if not xs or n == 0: + return [[]] + if len(xs) == n: + return [xs] + + rs = [] + x = xs[0] + for r in choose(xs[1:], n - 1): + rs.append([x] + r) + for r in choose(xs[1:], n): + rs.append(r) + return rs + + +def pairs(xs): + if len(xs) == 2: + return [xs] + r = [] + a = xs[0] + for b in xs[1:]: + r.append([a, b]) + + return r + pairs(xs[1:]) + + +def can_form_squares(pair): + xs, ys = pair + + # replace 9s with 6s + for d, e in [(0, 1), (0, 4), (0, 6), (1, 6), + (2, 5), (3, 6), (4, 6), (6, 4), (8, 1)]: + if d in xs and e in ys: + continue + elif e in xs and d in ys: + continue + return False + return True + + +def euler_090(): + # replace 9s with 6s to handle "up-side down usage of 6" without hassle + xs = list(range(0, 9)) + [6] + + cs = choose(xs, 6) + ps = pairs(cs) + count = 0 + for p in ps: + if can_form_squares(p): + count += 1 + return count + + +if __name__ == "__main__": + solution = euler_090() + print("e090.py: " + str(solution)) + assert(solution == 1217) +