4-rings or 4-squares puzzle
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")
}
}
# TASK
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:
Last updated
Was this helpful?