diff options
| author | Abigail <abigail@abigail.be> | 2021-04-24 16:27:46 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-04-24 16:27:46 +0200 |
| commit | 090b25b75556983567c32a01a8c8353bd508ff11 (patch) | |
| tree | 9ca89ed9d4dbf8867efbc2cd78e9f080b0d422f6 | |
| parent | 8acd1c3c19a89141559a027a711ef93b28c06438 (diff) | |
| download | perlweeklychallenge-club-090b25b75556983567c32a01a8c8353bd508ff11.tar.gz perlweeklychallenge-club-090b25b75556983567c32a01a8c8353bd508ff11.tar.bz2 perlweeklychallenge-club-090b25b75556983567c32a01a8c8353bd508ff11.zip | |
Bash solution for week 109, part 2
| -rw-r--r-- | challenge-109/abigail/bash/ch-2.sh | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/challenge-109/abigail/bash/ch-2.sh b/challenge-109/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..175ddf807d --- /dev/null +++ b/challenge-109/abigail/bash/ch-2.sh @@ -0,0 +1,83 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +SIZE=7 + +# +# Just try all possibilities, but continue as soon as we know +# this cannot lead to a solution. +# +# We use 7 variables, a_i .. g_i indicating the indices of of +# the numbers we try for a .. g. We use nested loops to iterate +# over all the possibilties. We continue with the next iteration +# of a loop if: +# +# - We pick an index which is already taken by an outer loop. +# - We have a mismatch in the sums in the squares: +# + We define a target = numbers [a_i] + numbers [b_i] +# + After picking c_i and d_i, we check whether +# numbers [b_i] + numbers [c_i] + numbers [d_i] equals target. +# + After picking e_i and f_i, we check whether +# numbers [d_i] + numbers [e_i] + numbers [f_i] equals target. +# + After picking g_i, we check whether +# numbers [f_i] + numbers [g_i] equals target. +# + If we pass all tests, we have a solution. +# + +while read -a numbers +do for ((a_i = 0; a_i < SIZE; a_i ++)) + do for ((b_i = 0; b_i < SIZE; b_i ++)) + do if ((a_i == b_i)) + then continue + fi + ((target = numbers[a_i] + numbers[b_i])) + for ((c_i = 0; c_i < SIZE; c_i ++)) + do if ((a_i == c_i || b_i == c_i)) + then continue + fi + for ((d_i = 0; d_i < SIZE; d_i ++)) + do if ((a_i == d_i || b_i == d_i || c_i == d_i || + target != numbers[b_i] + numbers[c_i] + + numbers[d_i])) + then continue + fi + for ((e_i = 0; e_i < SIZE; e_i ++)) + do if ((a_i == e_i || b_i == e_i || + c_i == e_i || d_i == e_i)) + then continue + fi + for ((f_i = 0; f_i < SIZE; f_i ++)) + do if ((a_i == f_i || b_i == f_i || + c_i == f_i || d_i == f_i || + e_i == f_i || + target != numbers[d_i] + numbers[e_i] + + numbers[f_i])) + then continue + fi + for ((g_i = 0; g_i < SIZE; g_i ++)) + do if ((a_i == g_i || b_i == g_i || + c_i == g_i || d_i == g_i || + e_i == g_i || f_i == g_i || + target != numbers[f_i] + numbers[g_i])) + then continue + fi + printf "%d %d %d %d %d %d %d\n" \ + ${numbers[$a_i]} ${numbers[$b_i]} \ + ${numbers[$c_i]} ${numbers[$d_i]} \ + ${numbers[$e_i]} ${numbers[$f_i]} \ + ${numbers[$g_i]} + done + done + done + done + done + done + done +done |
