func four_squares (list, unique=true, show=true) {
var solutions = []
func check(c) {
solutions << c if ([
c[0] + c[1],
c[1] + c[2] + c[3],
c[3] + c[4] + c[5],
c[5] + c[6],
].uniq.len == 1)
}
if (unique) {
list.combinations(7, {|*a|
a.permutations { |*c|
check(c)
}
})
} else {
7.of { list }.cartesian {|*c|
check(c)
}
}
say (solutions.len,
(unique ? ' ' : ' non-'),
"unique solutions found using #{list.join(', ')}.\n")
if (show) {
var f = "%#{list.max.len+1}s"
say ("\n".join(
('a'..'g').map{f % _}.join,
solutions.map{ .map{f % _}.join }...
), "\n")
}
}
four_squares(@(1..7))
four_squares(@(3..9))
four_squares([8, 9, 11, 12, 17, 18, 20, 21])
four_squares(@(0..9), unique: false, show: false)
Output:
8 unique solutions found using 1, 2, 3, 4, 5, 6, 7.
a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
4 unique solutions found using 3, 4, 5, 6, 7, 8, 9.
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
8 unique solutions found using 8, 9, 11, 12, 17, 18, 20, 21.
a b c d e f g
17 21 8 9 11 18 20
20 18 11 9 8 21 17
17 21 9 8 12 18 20
20 18 8 12 9 17 21
20 18 12 8 9 21 17
21 17 9 12 8 18 20
20 18 11 9 12 17 21
21 17 12 9 11 18 20
2860 non-unique solutions found using 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.