Topswops: Difference between revisions

Add Rust implementation
m (→‎{{header|REXX}}: added a comment and changed whitesdpace.)
(Add Rust implementation)
Line 2,153:
puts "%2d : %d" % [i, topswops(i)]
end</lang>
 
=={{header|Rust}}==
<lang rust>
use itertools::Itertools;
 
fn solve(deck: &[usize]) -> usize {
let mut counter = 0_usize;
let mut shuffle = deck.to_vec();
loop {
let p0 = shuffle[0];
if p0 == 1 {
break;
}
shuffle[..p0].reverse();
counter += 1;
}
 
counter
}
 
// this is a naive method which tries all permutations and works up to ~12 cards
fn topswops(number: usize) -> usize {
(1..=number)
.permutations(number)
.fold(0_usize, |mut acc, p| {
let steps = solve(&p);
if steps > acc {
acc = steps;
}
acc
})
}
fn main() {
(1_usize..=10).for_each(|x| println!("{}: {}", x, topswops(x)));
}
</lang>
{{out}}
<pre>
1: 0
2: 1
3: 2
4: 4
5: 7
6: 10
7: 16
8: 22
9: 30
10: 38
</pre>
 
=={{header|Scala}}==
Anonymous user