# Project Euler 213
# Expected empty squares after 50 flea jumps on 30x30.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function idx(x: i32, y: i32, h: i32) -> i64 {
return (x as i64) * (h as i64) + (y as i64)
}
function main() -> i32 {
let W: i32 = 30
let H: i32 = 30
let R: i32 = 50
let N: i64 = (W as i64) * (H as i64)
let empty: ptr<f64> = calloc(N, 8)
let cur: ptr<f64> = calloc(N, 8)
let nxt: ptr<f64> = calloc(N, 8)
if empty == null || cur == null || nxt == null { return 1 }
let mut i: i64 = 0
while i < N {
empty[i] = 1.0
i = i + 1
}
let max_x: i32 = W / 2
let max_y: i32 = H / 2
let mut sx: i32 = 0
while sx < max_x {
let mut sy: i32 = 0
while sy < max_y {
i = 0
while i < N {
cur[i] = 0.0
i = i + 1
}
cur[idx(sx, sy, H)] = 1.0
let mut r: i32 = 0
while r < R {
i = 0
while i < N {
nxt[i] = 0.0
i = i + 1
}
let mut x: i32 = 0
while x < W {
let mut y: i32 = 0
while y < H {
let p: f64 = cur[idx(x, y, H)]
if p != 0.0 {
let mut dirs: i32 = 4
if x == 0 || x == W - 1 { dirs = dirs - 1 }
if y == 0 || y == H - 1 { dirs = dirs - 1 }
let prob: f64 = p / (dirs as f64)
if x > 0 { nxt[idx(x - 1, y, H)] = nxt[idx(x - 1, y, H)] + prob }
if x < W - 1 { nxt[idx(x + 1, y, H)] = nxt[idx(x + 1, y, H)] + prob }
if y > 0 { nxt[idx(x, y - 1, H)] = nxt[idx(x, y - 1, H)] + prob }
if y < H - 1 { nxt[idx(x, y + 1, H)] = nxt[idx(x, y + 1, H)] + prob }
}
y = y + 1
}
x = x + 1
}
i = 0
while i < N {
cur[i] = nxt[i]
i = i + 1
}
r = r + 1
}
let mut x2: i32 = 0
while x2 < W {
let mut y2: i32 = 0
while y2 < H {
let e: i64 = idx(x2, y2, H)
empty[e] = empty[e] * (1.0 - cur[e])
empty[e] = empty[e] * (1.0 - cur[idx(W - 1 - x2, y2, H)])
empty[e] = empty[e] * (1.0 - cur[idx(x2, H - 1 - y2, H)])
empty[e] = empty[e] * (1.0 - cur[idx(W - 1 - x2, H - 1 - y2, H)])
y2 = y2 + 1
}
x2 = x2 + 1
}
sy = sy + 1
}
sx = sx + 1
}
let mut result: f64 = 0.0
i = 0
while i < N {
result = result + empty[i]
i = i + 1
}
printf("%.6f\n", result)
free(empty); free(cur); free(nxt)
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 idx_i32_i32_i32(int32_t x, int32_t y, int32_t h);
int32_t main(void);
int64_t idx_i32_i32_i32(int32_t x, int32_t y, int32_t h) {
return ((((int64_t)(x)) * ((int64_t)(h))) + ((int64_t)(y)));
}
int32_t main(void) {
int32_t W = 30;
int32_t H = 30;
int32_t R = 50;
int64_t N = (((int64_t)(W)) * ((int64_t)(H)));
double* empty = (double*)(calloc(N, 8));
double* cur = (double*)(calloc(N, 8));
double* nxt = (double*)(calloc(N, 8));
if (((empty == NULL || cur == NULL) || nxt == NULL)) {
return 1;
}
int64_t i = 0;
while (i < N) {
empty[i] = 1.0;
i = (i + 1);
}
int32_t max_x = FLOW_CHECKED_DIV((W), (2));
int32_t max_y = FLOW_CHECKED_DIV((H), (2));
int32_t sx = 0;
while (sx < max_x) {
int32_t sy = 0;
while (sy < max_y) {
i = 0;
while (i < N) {
cur[i] = 0.0;
i = (i + 1);
}
cur[idx_i32_i32_i32(sx, sy, H)] = 1.0;
int32_t r = 0;
while (r < R) {
i = 0;
while (i < N) {
nxt[i] = 0.0;
i = (i + 1);
}
int32_t x = 0;
while (x < W) {
int32_t y = 0;
while (y < H) {
double p = cur[idx_i32_i32_i32(x, y, H)];
if (p != 0.0) {
int32_t dirs = 4;
if ((x == 0 || x == (W - 1))) {
dirs = (dirs - 1);
}
if ((y == 0 || y == (H - 1))) {
dirs = (dirs - 1);
}
double prob = (p / ((double)(dirs)));
if (x > 0) {
nxt[idx_i32_i32_i32((x - 1), y, H)] = (nxt[idx_i32_i32_i32((x - 1), y, H)] + prob);
}
if (x < (W - 1)) {
nxt[idx_i32_i32_i32((x + 1), y, H)] = (nxt[idx_i32_i32_i32((x + 1), y, H)] + prob);
}
if (y > 0) {
nxt[idx_i32_i32_i32(x, (y - 1), H)] = (nxt[idx_i32_i32_i32(x, (y - 1), H)] + prob);
}
if (y < (H - 1)) {
nxt[idx_i32_i32_i32(x, (y + 1), H)] = (nxt[idx_i32_i32_i32(x, (y + 1), H)] + prob);
}
}
y = (y + 1);
}
x = (x + 1);
}
i = 0;
while (i < N) {
cur[i] = nxt[i];
i = (i + 1);
}
r = (r + 1);
}
int32_t x2 = 0;
while (x2 < W) {
int32_t y2 = 0;
while (y2 < H) {
int64_t e = idx_i32_i32_i32(x2, y2, H);
empty[e] = (empty[e] * (1.0 - cur[e]));
empty[e] = (empty[e] * (1.0 - cur[idx_i32_i32_i32(((W - 1) - x2), y2, H)]));
empty[e] = (empty[e] * (1.0 - cur[idx_i32_i32_i32(x2, ((H - 1) - y2), H)]));
empty[e] = (empty[e] * (1.0 - cur[idx_i32_i32_i32(((W - 1) - x2), ((H - 1) - y2), H)]));
y2 = (y2 + 1);
}
x2 = (x2 + 1);
}
sy = (sy + 1);
}
sx = (sx + 1);
}
double result = 0.0;
i = 0;
while (i < N) {
result = (result + empty[i]);
i = (i + 1);
}
printf("%.6f\n", result);
free(empty);
free(cur);
free(nxt);
return 0;
}