aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-04-24 17:58:13 +0200
committerAbigail <abigail@abigail.be>2021-04-24 17:58:13 +0200
commit5aa51940f1fa28317d7fd16b1850bb8f0a00fe60 (patch)
tree464de45ca116950d823e5b3a3ebf5b4806be0308
parent090b25b75556983567c32a01a8c8353bd508ff11 (diff)
downloadperlweeklychallenge-club-5aa51940f1fa28317d7fd16b1850bb8f0a00fe60.tar.gz
perlweeklychallenge-club-5aa51940f1fa28317d7fd16b1850bb8f0a00fe60.tar.bz2
perlweeklychallenge-club-5aa51940f1fa28317d7fd16b1850bb8f0a00fe60.zip
Python solution for week 109, part 2
-rw-r--r--challenge-109/abigail/README.md2
-rw-r--r--challenge-109/abigail/python/ch-2.py66
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-109/abigail/README.md b/challenge-109/abigail/README.md
index 6840f8e103..81fa6c0cc4 100644
--- a/challenge-109/abigail/README.md
+++ b/challenge-109/abigail/README.md
@@ -95,7 +95,9 @@ Output:
* [Bash](bash/ch-2.sh)
* [C](c/ch-2.c)
* [Lua](lua/ch-2.lua)
+* [Node.js](node/ch-2.js)
* [Perl](perl/ch-2.pl)
+* [Python](python/ch-2.py)
### Blog
diff --git a/challenge-109/abigail/python/ch-2.py b/challenge-109/abigail/python/ch-2.py
new file mode 100644
index 0000000000..c17ac94211
--- /dev/null
+++ b/challenge-109/abigail/python/ch-2.py
@@ -0,0 +1,66 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as: python ch-2.py < input-file
+#
+
+import fileinput
+
+SIZE = 7
+
+fmt = "{:d} {:d} {:d} {:d} {:d} {:d} {:d}"
+
+#
+# Brute forcing all possiblities, with an early return
+#
+for line in fileinput . input ():
+ numbers = list (map (lambda x: int (x), line . split ()))
+
+ for a_i in range (0, SIZE):
+ for b_i in range (0, SIZE):
+ if a_i == b_i:
+ continue
+ target = numbers [a_i] + numbers [b_i]
+
+ for c_i in range (0, SIZE):
+ if c_i == a_i or c_i == b_i:
+ continue
+
+ for d_i in range (0, SIZE):
+ if d_i == a_i or d_i == b_i or d_i == c_i:
+ continue
+
+ if target != numbers [b_i] + numbers [c_i] + numbers [d_i]:
+ continue
+
+ for e_i in range (0, SIZE):
+ if e_i == a_i or e_i == b_i or e_i == c_i or \
+ e_i == d_i:
+ continue
+
+ for f_i in range (0, SIZE):
+ if f_i == a_i or f_i == b_i or f_i == c_i or \
+ f_i == d_i or f_i == e_i:
+ continue
+ if target != numbers [d_i] + numbers [e_i] + \
+ numbers [f_i]:
+ continue
+
+ for g_i in range (0, SIZE):
+ if g_i == a_i or g_i == b_i or g_i == c_i or \
+ g_i == d_i or g_i == e_i or g_i == f_i:
+ continue
+ if target != numbers [f_i] + numbers [g_i]:
+ continue
+
+ print (fmt . format (numbers [a_i],
+ numbers [b_i],
+ numbers [c_i],
+ numbers [d_i],
+ numbers [e_i],
+ numbers [f_i],
+ numbers [g_i]))