# Project Euler 367
# Expected 3-shuffle bozo-sort steps for n=11, rounded nearest.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function floor(x: f64) -> f64
}
function factorial_f(n: i64) -> f64 {
let mut r: f64 = 1.0
let mut i: i64 = 2
while i <= n {
r = r * (i as f64)
i = i + 1
}
return r
}
function dim_part(parts: ptr<i32>, plen: i64) -> f64 {
let mut n: i64 = 0
let mut i: i64 = 0
while i < plen {
n = n + (parts[i] as i64)
i = i + 1
}
if n == 0 { return 1.0 }
let mut maxc: i64 = 0
i = 0
while i < plen {
if (parts[i] as i64) > maxc { maxc = parts[i] as i64 }
i = i + 1
}
let col_lens: ptr<i32> = calloc(maxc, 4)
let mut c: i64 = 0
while c < maxc {
let mut cnt: i64 = 0
i = 0
while i < plen {
if (parts[i] as i64) > c { cnt = cnt + 1 }
i = i + 1
}
col_lens[c] = cnt as i32
c = c + 1
}
let mut prod: f64 = 1.0
i = 0
while i < plen {
let ln: i64 = parts[i] as i64
c = 0
while c < ln {
let hook: f64 = ((ln - c) + (col_lens[c] as i64) - i - 1) as f64
prod = prod * hook
c = c + 1
}
i = i + 1
}
free(col_lens)
return factorial_f(n) / prod
}
function cell_in(cells: ptr<i32>, ncells: i64, r: i32, c: i32) -> bool {
let mut i: i64 = 0
while i < ncells {
if cells[2 * i] == r && cells[2 * i + 1] == c { return true }
i = i + 1
}
return false
}
function is_connected(cells: ptr<i32>, ncells: i64) -> bool {
if ncells == 0 { return false }
let seen: ptr<i8> = calloc(ncells, 1)
let stack: ptr<i64> = calloc(ncells, 8)
stack[0] = 0
seen[0] = 1
let mut top: i64 = 1
let mut count: i64 = 1
while top > 0 {
top = top - 1
let idx: i64 = stack[top]
let r: i32 = cells[2 * idx]
let c: i32 = cells[2 * idx + 1]
let mut d: i64 = 0
while d < 4 {
let mut nr: i32 = r
let mut nc: i32 = c
match d {
0 => { nr = r + 1 }
1 => { nr = r - 1 }
2 => { nc = c + 1 }
_ => { nc = c - 1 }
}
let mut j: i64 = 0
while j < ncells {
if cells[2 * j] == nr && cells[2 * j + 1] == nc {
if seen[j] == 0 {
seen[j] = 1
stack[top] = j
top = top + 1
count = count + 1
}
break
}
j = j + 1
}
d = d + 1
}
}
free(stack)
free(seen)
return count == ncells
}
function has_2x2(cells: ptr<i32>, ncells: i64) -> bool {
let mut i: i64 = 0
while i < ncells {
let r: i32 = cells[2 * i]
let c: i32 = cells[2 * i + 1]
if cell_in(cells, ncells, r + 1, c) && cell_in(cells, ncells, r, c + 1) && cell_in(cells, ncells, r + 1, c + 1) {
return true
}
i = i + 1
}
return false
}
function remaining_partition(parts: ptr<i32>, plen: i64, cells: ptr<i32>, ncells: i64,
out: ptr<i32>, out_len: ptr<i64>) -> bool {
let mut new_len: i64 = 0
let mut r: i64 = 0
while r < plen {
let ln: i64 = parts[r] as i64
let mut max_rem: i64 = 0 - 1
let mut c: i64 = 0
while c < ln {
if !cell_in(cells, ncells, r as i32, c as i32) {
if c > max_rem { max_rem = c }
}
c = c + 1
}
let mut nrow: i64 = 0
if max_rem >= 0 {
nrow = max_rem + 1
c = 0
while c < nrow {
if cell_in(cells, ncells, r as i32, c as i32) {
return false
}
c = c + 1
}
}
out[new_len] = nrow as i32
new_len = new_len + 1
r = r + 1
}
while new_len > 0 && out[new_len - 1] == 0 {
new_len = new_len - 1
}
let mut i: i64 = 0
while i + 1 < new_len {
if out[i] < out[i + 1] { return false }
i = i + 1
}
out_len[0] = new_len
return true
}
function character_cycle_k(parts: ptr<i32>, plen: i64, k: i64) -> f64 {
let cells_all: ptr<i32> = calloc(2 * 64, 4)
let mut nall: i64 = 0
let mut r: i64 = 0
while r < plen {
let mut c: i64 = 0
while c < (parts[r] as i64) {
cells_all[2 * nall] = r as i32
cells_all[2 * nall + 1] = c as i32
nall = nall + 1
c = c + 1
}
r = r + 1
}
if k > nall {
free(cells_all)
return 0.0
}
let mut total: f64 = 0.0
let limit: i64 = 1 << nall
let mut mask: i64 = 0
while mask < limit {
let mut bits: i64 = 0
let mut t: i64 = mask
while t > 0 {
bits = bits + (t & 1)
t = t >> 1
}
if bits == k {
let strip: ptr<i32> = calloc(2 * k, 4)
let mut si: i64 = 0
let mut i: i64 = 0
while i < nall {
if ((mask >> i) & 1) == 1 {
strip[2 * si] = cells_all[2 * i]
strip[2 * si + 1] = cells_all[2 * i + 1]
si = si + 1
}
i = i + 1
}
let out: ptr<i32> = calloc(16, 4)
let out_len: ptr<i64> = calloc(1, 8)
if remaining_partition(parts, plen, strip, k, out, out_len) {
if is_connected(strip, k) {
if !has_2x2(strip, k) {
let mut rows_used: i64 = 0
let seen_r: ptr<i8> = calloc(16, 1)
i = 0
while i < k {
let rr: i32 = strip[2 * i]
if seen_r[rr] == 0 {
seen_r[rr] = 1
rows_used = rows_used + 1
}
i = i + 1
}
free(seen_r)
let height: i64 = rows_used - 1
let dnu: f64 = dim_part(out, out_len[0])
let mut sign: f64 = 1.0
if (height % 2) == 1 { sign = 0.0 - 1.0 }
total = total + sign * dnu
}
}
}
free(out_len)
free(out)
free(strip)
}
mask = mask + 1
}
free(cells_all)
return total
}
function process_partition(parts: ptr<i32>, plen: i64, n: i64) -> f64 {
if plen == 1 && (parts[0] as i64) == n { return 0.0 }
let d: f64 = dim_part(parts, plen)
let chi2: f64 = character_cycle_k(parts, plen, 2)
let chi3: f64 = character_cycle_k(parts, plen, 3)
let r2: f64 = chi2 / d
let r3: f64 = chi3 / d
let lam: f64 = 1.0 / 6.0 + 0.5 * r2 + (1.0 / 3.0) * r3
return (d * d) / (1.0 - lam)
}
function gen_and_sum(parts: ptr<i32>, plen: i64, rem: i64, maxp: i64, n: i64) -> f64 {
if rem == 0 {
return process_partition(parts, plen, n)
}
let mut total: f64 = 0.0
let mut first: i64 = maxp
if first > rem { first = rem }
while first >= 1 {
parts[plen] = first as i32
total = total + gen_and_sum(parts, plen + 1, rem - first, first, n)
first = first - 1
}
return total
}
function main() -> i32 {
let parts: ptr<i32> = calloc(11, 4)
let x: f64 = gen_and_sum(parts, 0, 11, 11, 11)
free(parts)
let mut ans: i64 = floor(x) as i64
let frac: f64 = x - (ans as f64)
if frac >= 0.5 { ans = ans + 1 }
printf("%lld\n", ans)
return 0
}
Generated C
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
while (flow_temp_head) {
flow_temp_node* n = flow_temp_head;
flow_temp_head = n->next;
free(n);
}
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
if (!node) return NULL;
node->next = flow_temp_head;
flow_temp_head = node;
if (!flow_temp_atexit_set) {
flow_temp_atexit_set = 1;
atexit(flow_temp_free_all);
}
return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
char* r = (char*)flow_temp_alloc(la + lb + 1);
if (!r) return NULL;
if (la) memcpy(r, a, la);
if (lb) memcpy(r + la, b, lb);
r[la + lb] = '\0';
return r;
}
#define __flow_in_arr(arr, val) __extension__ ({ \
int _found = 0; \
size_t _n = sizeof(arr)/sizeof((arr)[0]); \
for (size_t _i = 0; _i < _n; _i++) { \
if ((arr)[_i] == (val)) { _found = 1; break; } \
} _found; })
/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
abort();
#if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")
#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#include <math.h>
void* _ui_state = NULL;
static inline float i32_to_f32(int32_t v) { return (float)v; }
/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }
double factorial_f_i64(int64_t n);
double dim_part_ptr_i32_i64(int32_t* parts, int64_t plen);
bool cell_in_ptr_i32_i64_i32_i32(int32_t* cells, int64_t ncells, int32_t r, int32_t c);
bool is_connected_ptr_i32_i64(int32_t* cells, int64_t ncells);
bool has_2x2_ptr_i32_i64(int32_t* cells, int64_t ncells);
bool remaining_partition_ptr_i32_i64_ptr_i32_i64_ptr_i32_ptr_i64(int32_t* parts, int64_t plen, int32_t* cells, int64_t ncells, int32_t* out, int64_t* out_len);
double character_cycle_k_ptr_i32_i64_i64(int32_t* parts, int64_t plen, int64_t k);
double process_partition_ptr_i32_i64_i64(int32_t* parts, int64_t plen, int64_t n);
double gen_and_sum_ptr_i32_i64_i64_i64_i64(int32_t* parts, int64_t plen, int64_t rem, int64_t maxp, int64_t n);
int32_t main(void);
double factorial_f_i64(int64_t n) {
double r = 1.0;
int64_t i = 2;
while (i <= n) {
r = (r * ((double)(i)));
i = (i + 1);
}
return r;
}
double dim_part_ptr_i32_i64(int32_t* parts, int64_t plen) {
int64_t n = 0;
int64_t i = 0;
while (i < plen) {
n = (n + ((int64_t)(parts[i])));
i = (i + 1);
}
if (n == 0) {
return 1.0;
}
int64_t maxc = 0;
i = 0;
while (i < plen) {
if (((int64_t)(parts[i])) > maxc) {
maxc = ((int64_t)(parts[i]));
}
i = (i + 1);
}
int32_t* col_lens = (int32_t*)(calloc(maxc, 4));
int64_t c = 0;
while (c < maxc) {
int64_t cnt = 0;
i = 0;
while (i < plen) {
if (((int64_t)(parts[i])) > c) {
cnt = (cnt + 1);
}
i = (i + 1);
}
col_lens[c] = ((int32_t)(cnt));
c = (c + 1);
}
double prod = 1.0;
i = 0;
while (i < plen) {
int64_t ln = ((int64_t)(parts[i]));
c = 0;
while (c < ln) {
double hook = ((double)(((((ln - c) + ((int64_t)(col_lens[c]))) - i) - 1)));
prod = (prod * hook);
c = (c + 1);
}
i = (i + 1);
}
free(col_lens);
return (factorial_f_i64(n) / prod);
}
bool cell_in_ptr_i32_i64_i32_i32(int32_t* cells, int64_t ncells, int32_t r, int32_t c) {
int64_t i = 0;
while (i < ncells) {
if ((cells[(2 * i)] == r && cells[((2 * i) + 1)] == c)) {
return 1;
}
i = (i + 1);
}
return 0;
}
bool is_connected_ptr_i32_i64(int32_t* cells, int64_t ncells) {
if (ncells == 0) {
return 0;
}
int8_t* seen = (int8_t*)(calloc(ncells, 1));
int64_t* stack = (int64_t*)(calloc(ncells, 8));
stack[0] = 0;
seen[0] = 1;
int64_t top = 1;
int64_t count = 1;
while (top > 0) {
top = (top - 1);
int64_t idx = stack[top];
int32_t r = cells[(2 * idx)];
int32_t c = cells[((2 * idx) + 1)];
int64_t d = 0;
while (d < 4) {
int32_t nr = r;
int32_t nc = c;
{ // match block
if ((d) == 0) {
nr = (r + 1);
} else if ((d) == 1) {
nr = (r - 1);
} else if ((d) == 2) {
nc = (c + 1);
} else { // exhaustive
nc = (c - 1);
}
} // end match
int64_t j = 0;
while (j < ncells) {
if ((cells[(2 * j)] == nr && cells[((2 * j) + 1)] == nc)) {
if (seen[j] == 0) {
seen[j] = 1;
stack[top] = j;
top = (top + 1);
count = (count + 1);
}
break;
}
j = (j + 1);
}
d = (d + 1);
}
}
free(stack);
free(seen);
return count == ncells;
}
bool has_2x2_ptr_i32_i64(int32_t* cells, int64_t ncells) {
int64_t i = 0;
while (i < ncells) {
int32_t r = cells[(2 * i)];
int32_t c = cells[((2 * i) + 1)];
if (((cell_in_ptr_i32_i64_i32_i32(cells, ncells, (r + 1), c) && cell_in_ptr_i32_i64_i32_i32(cells, ncells, r, (c + 1))) && cell_in_ptr_i32_i64_i32_i32(cells, ncells, (r + 1), (c + 1)))) {
return 1;
}
i = (i + 1);
}
return 0;
}
bool remaining_partition_ptr_i32_i64_ptr_i32_i64_ptr_i32_ptr_i64(int32_t* parts, int64_t plen, int32_t* cells, int64_t ncells, int32_t* out, int64_t* out_len) {
int64_t new_len = 0;
int64_t r = 0;
while (r < plen) {
int64_t ln = ((int64_t)(parts[r]));
int64_t max_rem = (0 - 1);
int64_t c = 0;
while (c < ln) {
if ((!(cell_in_ptr_i32_i64_i32_i32(cells, ncells, ((int32_t)(r)), ((int32_t)(c)))))) {
if (c > max_rem) {
max_rem = c;
}
}
c = (c + 1);
}
int64_t nrow = 0;
if (max_rem >= 0) {
nrow = (max_rem + 1);
c = 0;
while (c < nrow) {
if (cell_in_ptr_i32_i64_i32_i32(cells, ncells, ((int32_t)(r)), ((int32_t)(c)))) {
return 0;
}
c = (c + 1);
}
}
out[new_len] = ((int32_t)(nrow));
new_len = (new_len + 1);
r = (r + 1);
}
while ((new_len > 0 && out[(new_len - 1)] == 0)) {
new_len = (new_len - 1);
}
int64_t i = 0;
while ((i + 1) < new_len) {
if (out[i] < out[(i + 1)]) {
return 0;
}
i = (i + 1);
}
out_len[0] = new_len;
return 1;
}
double character_cycle_k_ptr_i32_i64_i64(int32_t* parts, int64_t plen, int64_t k) {
int32_t* cells_all = (int32_t*)(calloc((2 * 64), 4));
int64_t nall = 0;
int64_t r = 0;
while (r < plen) {
int64_t c = 0;
while (c < ((int64_t)(parts[r]))) {
cells_all[(2 * nall)] = ((int32_t)(r));
cells_all[((2 * nall) + 1)] = ((int32_t)(c));
nall = (nall + 1);
c = (c + 1);
}
r = (r + 1);
}
if (k > nall) {
free(cells_all);
return 0.0;
}
double total = 0.0;
int64_t limit = FLOW_CHECKED_SHL((1), (nall));
int64_t mask = 0;
while (mask < limit) {
int64_t bits = 0;
int64_t t = mask;
while (t > 0) {
bits = (bits + (t & 1));
t = FLOW_CHECKED_SHR((t), (1));
}
if (bits == k) {
int32_t* strip = (int32_t*)(calloc((2 * k), 4));
int64_t si = 0;
int64_t i = 0;
while (i < nall) {
if ((FLOW_CHECKED_SHR((mask), (i)) & 1) == 1) {
strip[(2 * si)] = cells_all[(2 * i)];
strip[((2 * si) + 1)] = cells_all[((2 * i) + 1)];
si = (si + 1);
}
i = (i + 1);
}
int32_t* out = (int32_t*)(calloc(16, 4));
int64_t* out_len = (int64_t*)(calloc(1, 8));
if (remaining_partition_ptr_i32_i64_ptr_i32_i64_ptr_i32_ptr_i64(parts, plen, strip, k, out, out_len)) {
if (is_connected_ptr_i32_i64(strip, k)) {
if ((!(has_2x2_ptr_i32_i64(strip, k)))) {
int64_t rows_used = 0;
int8_t* seen_r = (int8_t*)(calloc(16, 1));
i = 0;
while (i < k) {
int32_t rr = strip[(2 * i)];
if (seen_r[rr] == 0) {
seen_r[rr] = 1;
rows_used = (rows_used + 1);
}
i = (i + 1);
}
free(seen_r);
int64_t height = (rows_used - 1);
double dnu = dim_part_ptr_i32_i64(out, out_len[0]);
double sign = 1.0;
if (FLOW_CHECKED_MOD((height), (2)) == 1) {
sign = (0.0 - 1.0);
}
total = (total + (sign * dnu));
}
}
}
free(out_len);
free(out);
free(strip);
}
mask = (mask + 1);
}
free(cells_all);
return total;
}
double process_partition_ptr_i32_i64_i64(int32_t* parts, int64_t plen, int64_t n) {
if ((plen == 1 && ((int64_t)(parts[0])) == n)) {
return 0.0;
}
double d = dim_part_ptr_i32_i64(parts, plen);
double chi2 = character_cycle_k_ptr_i32_i64_i64(parts, plen, 2);
double chi3 = character_cycle_k_ptr_i32_i64_i64(parts, plen, 3);
double r2 = (chi2 / d);
double r3 = (chi3 / d);
double lam = (((1.0 / 6.0) + (0.5 * r2)) + ((1.0 / 3.0) * r3));
return ((d * d) / (1.0 - lam));
}
double gen_and_sum_ptr_i32_i64_i64_i64_i64(int32_t* parts, int64_t plen, int64_t rem, int64_t maxp, int64_t n) {
if (rem == 0) {
return process_partition_ptr_i32_i64_i64(parts, plen, n);
}
double total = 0.0;
int64_t first = maxp;
if (first > rem) {
first = rem;
}
while (first >= 1) {
parts[plen] = ((int32_t)(first));
total = (total + gen_and_sum_ptr_i32_i64_i64_i64_i64(parts, (plen + 1), (rem - first), first, n));
first = (first - 1);
}
return total;
}
int32_t main(void) {
int32_t* parts = (int32_t*)(calloc(11, 4));
double x = gen_and_sum_ptr_i32_i64_i64_i64_i64(parts, 0, 11, 11, 11);
free(parts);
int64_t ans = ((int64_t)(floor(x)));
double frac = (x - ((double)(ans)));
if (frac >= 0.5) {
ans = (ans + 1);
}
printf("%lld\n", ans);
return 0;
}