4-rings or 4-squares puzzle: Difference between revisions
Content added Content deleted
m (→{{header|Picat}}: Added out tag) |
No edit summary |
||
Line 6,044: | Line 6,044: | ||
C 9 7 5 6 A B |
C 9 7 5 6 A B |
||
4 unique solutions for [5,12]</pre> |
4 unique solutions for [5,12]</pre> |
||
=={{header|Vlang}}== |
|||
{{trans|Go}} |
|||
<lang vlang>fn main(){ |
|||
mut n, mut c := get_combs(1,7,true) |
|||
println("$n unique solutions in 1 to 7") |
|||
println(c) |
|||
n, c = get_combs(3,9,true) |
|||
println("$n unique solutions in 3 to 9") |
|||
println(c) |
|||
n, _ = get_combs(0,9,false) |
|||
println("$n non-unique solutions in 0 to 9") |
|||
} |
|||
fn get_combs(low int,high int,unique bool) (int, [][]int) { |
|||
mut num := 0 |
|||
mut valid_combs := [][]int{} |
|||
for a := low; a <= high; a++ { |
|||
for b := low; b <= high; b++ { |
|||
for c := low; c <= high; c++ { |
|||
for d := low; d <= high; d++ { |
|||
for e := low; e <= high; e++ { |
|||
for f := low; f <= high; f++ { |
|||
for g := low; g <= high; g++ { |
|||
if valid_comb(a,b,c,d,e,f,g) { |
|||
if !unique || is_unique(a,b,c,d,e,f,g) { |
|||
num++ |
|||
valid_combs << [a,b,c,d,e,f,g] |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
return num, valid_combs |
|||
} |
|||
fn is_unique(a int,b int,c int,d int,e int,f int,g int) bool { |
|||
mut data := map[int]int{} |
|||
data[a]++ |
|||
data[b]++ |
|||
data[c]++ |
|||
data[d]++ |
|||
data[e]++ |
|||
data[f]++ |
|||
data[g]++ |
|||
return data.len == 7 |
|||
} |
|||
fn valid_comb(a int,b int,c int,d int,e int,f int,g int) bool { |
|||
square1 := a + b |
|||
square2 := b + c + d |
|||
square3 := d + e + f |
|||
square4 := f + g |
|||
return square1 == square2 && square2 == square3 && square3 == square4 |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
8 unique solutions in 1 to 7 |
|||
[[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 in 3 to 9 |
|||
[[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]] |
|||
2860 non-unique solutions in 0 to 9 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |