# Project Euler 868
# Belfry Maths — Steinhaus–Johnson–Trotter rank of a word.
function sjt_rank(perm: ptr<i64>, n: i64) -> i64 {
if n <= 1 { return 0 }
let mut pos: i64 = 0
while pos < n {
if perm[pos] == n { break }
pos = pos + 1
}
# remove n into base[0..n-2]
let mut base: [i64; 32]
let mut j: i64 = 0
let mut i: i64 = 0
while i < n {
if i != pos {
base[j] = perm[i]
j = j + 1
}
i = i + 1
}
let r: i64 = sjt_rank(&base[0], n - 1)
let mut t: i64 = pos
if (r & 1) == 0 {
t = n - 1 - pos
}
return r * n + t
}
function swaps_to_reach(word: ptr<i8>, n: i64) -> i64 {
# Map letters to 1..n by sorted order
let mut letters: [i8; 32]
let mut i: i64 = 0
while i < n {
letters[i] = word[i]
i = i + 1
}
# insertion sort copy for ranking
let mut sorted: [i8; 32]
i = 0
while i < n {
sorted[i] = letters[i]
i = i + 1
}
let mut a: i64 = 1
while a < n {
let key: i8 = sorted[a]
let mut b: i64 = a
while b > 0 && sorted[b - 1] > key {
sorted[b] = sorted[b - 1]
b = b - 1
}
sorted[b] = key
a = a + 1
}
let mut perm: [i64; 32]
i = 0
while i < n {
let ch: i8 = letters[i]
let mut rank: i64 = 1
let mut j: i64 = 0
while j < n {
if sorted[j] == ch {
rank = j + 1
break
}
j = j + 1
}
perm[i] = rank
i = i + 1
}
return sjt_rank(&perm[0], n)
}
function main() -> i32 {
let word: ptr<i8> = "NOWPICKBELFRYMATHS"
printf("%lld\n", swaps_to_reach(word, 18))
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; }
int64_t sjt_rank_ptr_i64_i64(int64_t* perm, int64_t n);
int64_t swaps_to_reach_ptr_i8_i64(int8_t* word, int64_t n);
int32_t main(void);
int64_t sjt_rank_ptr_i64_i64(int64_t* perm, int64_t n) {
if (n <= 1) {
return 0;
}
int64_t pos = 0;
while (pos < n) {
if (perm[pos] == n) {
break;
}
pos = (pos + 1);
}
int64_t base[32];
int64_t j = 0;
int64_t i = 0;
while (i < n) {
if (i != pos) {
base[j] = perm[i];
j = (j + 1);
}
i = (i + 1);
}
int64_t r = sjt_rank_ptr_i64_i64((&(base[0])), (n - 1));
int64_t t = pos;
if ((r & 1) == 0) {
t = ((n - 1) - pos);
}
return ((r * n) + t);
}
int64_t swaps_to_reach_ptr_i8_i64(int8_t* word, int64_t n) {
int8_t letters[32];
int64_t i = 0;
while (i < n) {
letters[i] = word[i];
i = (i + 1);
}
int8_t sorted[32];
i = 0;
while (i < n) {
sorted[i] = (((unsigned)(i) < 32) ? letters[i] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(i), 32), flow_fault_handler("array index out of bounds"), letters[0]));
i = (i + 1);
}
int64_t a = 1;
while (a < n) {
int8_t key = (((unsigned)(a) < 32) ? sorted[a] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(a), 32), flow_fault_handler("array index out of bounds"), sorted[0]));
int64_t b = a;
while ((b > 0 && (((unsigned)((b - 1)) < 32) ? sorted[(b - 1)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((b - 1)), 32), flow_fault_handler("array index out of bounds"), sorted[0])) > key)) {
sorted[b] = (((unsigned)((b - 1)) < 32) ? sorted[(b - 1)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((b - 1)), 32), flow_fault_handler("array index out of bounds"), sorted[0]));
b = (b - 1);
}
sorted[b] = key;
a = (a + 1);
}
int64_t perm[32];
i = 0;
while (i < n) {
int8_t ch = (((unsigned)(i) < 32) ? letters[i] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(i), 32), flow_fault_handler("array index out of bounds"), letters[0]));
int64_t rank = 1;
int64_t j = 0;
while (j < n) {
if ((((unsigned)(j) < 32) ? sorted[j] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(j), 32), flow_fault_handler("array index out of bounds"), sorted[0])) == ch) {
rank = (j + 1);
break;
}
j = (j + 1);
}
perm[i] = rank;
i = (i + 1);
}
return sjt_rank_ptr_i64_i64((&(perm[0])), n);
}
int32_t main(void) {
int8_t* word = (int8_t*)("NOWPICKBELFRYMATHS");
printf("%lld\n", swaps_to_reach_ptr_i8_i64(word, 18));
return 0;
}