blob: 175ddf807dc2c3851ebf2d9f52172ab875b7f5df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
|