fannkuch redux
zo / tasks
program
fun main() { imu argv: []str = args(); imu n: int = if argv.len > 0 { match argv[0].parse_int() { Option::Some(value) => value, Option::None => 7, } } else { 7 }; mut perm_max: int = 1; for i := 1..n + 1 { perm_max *= i; } mut current: []int = []; mut temp: []int = []; mut count: []int = []; for i := 0..n { current.push(i); temp.push(0); count.push(0); } current.push(0); temp.push(0); count.push(0); mut checksum: int = 0; mut maxflips: int = 0; mut perm_index: int = 0; loop { if current[0] > 0 { for i := 0..n { temp[i] = current[i]; } mut flip_count: int = 1; mut first_value: int = current[0]; while temp[first_value] != 0 { imu new_first := temp[first_value]; temp[first_value] = first_value; if first_value > 2 { mut lo: int = 1; mut hi: int = first_value - 1; while lo < hi { imu swap := temp[lo]; temp[lo] = temp[hi]; temp[hi] = swap; lo += 1; hi -= 1; } } first_value = new_first; flip_count += 1; } if perm_index % 2 == 0 { checksum += flip_count; } else { checksum -= flip_count; } if flip_count > maxflips { maxflips = flip_count; } } if perm_index >= perm_max - 1 { break; } perm_index += 1; mut first_value: int = current[1]; current[1] = current[0]; current[0] = first_value; mut i: int = 1; while count[i] >= i { count[i] = 0; i += 1; imu new_first := current[1]; current[0] = new_first; for j := 1..i { current[j] = current[j + 1]; } current[i] = first_value; first_value = new_first; } count[i] += 1; } showln(checksum); showln("Pfannkuchen({n}) = {maxflips}"); }
output
228 Pfannkuchen(7) = 16