← All problems
Problem 205
P(Peter 9d4 beats Colin 6d6).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^3)O(n * s)
Space complexity O(1)O(s)
Approach Flow solution Dice distribution DP
Verdict Unknown
Flow source
# Project Euler 205
# P(Peter 9d4 beats Colin 6d6).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function roll_dp(dices: i32, sides: i32, count: ptr<i64>) -> void {
count[0] = 1
let mut d: i32 = 0
while d < dices {
let mut next: ptr<i64> = calloc(40, 8)
let mut t: i32 = 0
while t <= 36 {
if count[t] != 0 {
let mut s: i32 = 1
while s <= sides {
next[t + s] = next[t + s] + count[t]
s = s + 1
}
}
t = t + 1
}
t = 0
while t <= 36 {
count[t] = next[t]
t = t + 1
}
free(next)
d = d + 1
}
}
function main() -> i32 {
let peter: ptr<i64> = calloc(40, 8)
let colin: ptr<i64> = calloc(40, 8)
if peter == null || colin == null { return 1 }
roll_dp(9, 4, peter)
roll_dp(6, 6, colin)
let mut sum_p: f64 = 0.0
let mut sum_c: f64 = 0.0
let mut t: i32 = 0
while t <= 36 {
sum_p = sum_p + (peter[t] as f64)
sum_c = sum_c + (colin[t] as f64)
t = t + 1
}
let mut win: f64 = 0.0
t = 1
while t <= 36 {
let mut num_wins: f64 = 0.0
let mut u: i32 = 1
while u < t {
num_wins = num_wins + (colin[u] as f64)
u = u + 1
}
win = win + (num_wins / sum_c) * ((peter[t] as f64) / sum_p)
t = t + 1
}
printf("%.7f\n", win)
free(peter); free(colin)
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; }
void roll_dp_i32_i32_ptr_i64(int32_t dices, int32_t sides, int64_t* count);
int32_t main(void);
void roll_dp_i32_i32_ptr_i64(int32_t dices, int32_t sides, int64_t* count) {
count[0] = 1;
int32_t d = 0;
while (d < dices) {
int64_t* next = (int64_t*)(calloc(40, 8));
int32_t t = 0;
while (t <= 36) {
if (count[t] != 0) {
int32_t s = 1;
while (s <= sides) {
next[(t + s)] = (next[(t + s)] + count[t]);
s = (s + 1);
}
}
t = (t + 1);
}
t = 0;
while (t <= 36) {
count[t] = next[t];
t = (t + 1);
}
free(next);
d = (d + 1);
}
}
int32_t main(void) {
int64_t* peter = (int64_t*)(calloc(40, 8));
int64_t* colin = (int64_t*)(calloc(40, 8));
if ((peter == NULL || colin == NULL)) {
return 1;
}
roll_dp_i32_i32_ptr_i64(9, 4, peter);
roll_dp_i32_i32_ptr_i64(6, 6, colin);
double sum_p = 0.0;
double sum_c = 0.0;
int32_t t = 0;
while (t <= 36) {
sum_p = (sum_p + ((double)(peter[t])));
sum_c = (sum_c + ((double)(colin[t])));
t = (t + 1);
}
double win = 0.0;
t = 1;
while (t <= 36) {
double num_wins = 0.0;
int32_t u = 1;
while (u < t) {
num_wins = (num_wins + ((double)(colin[u])));
u = (u + 1);
}
win = (win + ((num_wins / sum_c) * (((double)(peter[t])) / sum_p)));
t = (t + 1);
}
printf("%.7f\n", win);
free(peter);
free(colin);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.7f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func @roll_dp(%arg0: i32, %arg1: i32, %arg2: !llvm.ptr) -> () {
%0 = arith.constant 1 : i32
%1 = arith.constant 0 : i32
%2 = arith.extsi %0 : i32 to i64
%3 = arith.extsi %1 : i32 to i64
%4 = llvm.getelementptr %arg2[%3] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %2, %4 : i64, !llvm.ptr
%5 = arith.constant 0 : i32
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i32 : (i64) -> !llvm.ptr
llvm.store %5, %7 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%8 = llvm.load %7 : !llvm.ptr -> i32
%9 = arith.cmpi slt, %8, %arg0 : i32
cf.cond_br %9, ^bb1, ^bb2
^bb1:
%11 = arith.constant 40 : i32
%12 = arith.constant 8 : i32
%13 = arith.extsi %11 : i32 to i64
%14 = arith.extsi %12 : i32 to i64
%10 = func.call @calloc(%13, %14) : (i64, i64) -> !llvm.ptr
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x !llvm.ptr : (i64) -> !llvm.ptr
llvm.store %10, %16 : !llvm.ptr, !llvm.ptr
%17 = arith.constant 0 : i32
%18 = llvm.mlir.constant(1 : i64) : i64
%19 = llvm.alloca %18 x i32 : (i64) -> !llvm.ptr
llvm.store %17, %19 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%20 = llvm.load %19 : !llvm.ptr -> i32
%21 = arith.constant 36 : i32
%22 = arith.cmpi sle, %20, %21 : i32
cf.cond_br %22, ^bb4, ^bb5
^bb4:
%24 = llvm.load %19 : !llvm.ptr -> i32
%25 = arith.extsi %24 : i32 to i64
%26 = llvm.getelementptr %arg2[%25] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%23 = llvm.load %26 : !llvm.ptr -> i64
%27 = arith.constant 0 : i32
%29 = arith.extsi %27 : i32 to i64
%28 = arith.cmpi ne, %23, %29 : i64
cf.cond_br %28, ^bb6, ^bb7
^bb6:
%30 = arith.constant 1 : i32
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i32 : (i64) -> !llvm.ptr
llvm.store %30, %32 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%33 = llvm.load %32 : !llvm.ptr -> i32
%34 = arith.cmpi sle, %33, %arg1 : i32
cf.cond_br %34, ^bb10, ^bb11
^bb10:
%36 = llvm.load %16 : !llvm.ptr -> !llvm.ptr
%37 = llvm.load %19 : !llvm.ptr -> i32
%38 = llvm.load %32 : !llvm.ptr -> i32
%39 = arith.addi %37, %38 : i32
%40 = arith.extsi %39 : i32 to i64
%41 = llvm.getelementptr %36[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%35 = llvm.load %41 : !llvm.ptr -> i64
%43 = llvm.load %19 : !llvm.ptr -> i32
%44 = arith.extsi %43 : i32 to i64
%45 = llvm.getelementptr %arg2[%44] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%42 = llvm.load %45 : !llvm.ptr -> i64
%46 = arith.addi %35, %42 : i64
%47 = llvm.load %16 : !llvm.ptr -> !llvm.ptr
%48 = llvm.load %19 : !llvm.ptr -> i32
%49 = llvm.load %32 : !llvm.ptr -> i32
%50 = arith.addi %48, %49 : i32
%51 = arith.extsi %50 : i32 to i64
%52 = llvm.getelementptr %47[%51] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %46, %52 : i64, !llvm.ptr
%53 = llvm.load %32 : !llvm.ptr -> i32
%54 = arith.constant 1 : i32
%55 = arith.addi %53, %54 : i32
llvm.store %55, %32 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%56 = llvm.load %19 : !llvm.ptr -> i32
%57 = arith.constant 1 : i32
%58 = arith.addi %56, %57 : i32
llvm.store %58, %19 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%59 = arith.constant 0 : i32
llvm.store %59, %19 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%60 = llvm.load %19 : !llvm.ptr -> i32
%61 = arith.constant 36 : i32
%62 = arith.cmpi sle, %60, %61 : i32
cf.cond_br %62, ^bb13, ^bb14
^bb13:
%64 = llvm.load %16 : !llvm.ptr -> !llvm.ptr
%65 = llvm.load %19 : !llvm.ptr -> i32
%66 = arith.extsi %65 : i32 to i64
%67 = llvm.getelementptr %64[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%63 = llvm.load %67 : !llvm.ptr -> i64
%68 = llvm.load %19 : !llvm.ptr -> i32
%69 = arith.extsi %68 : i32 to i64
%70 = llvm.getelementptr %arg2[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %63, %70 : i64, !llvm.ptr
%71 = llvm.load %19 : !llvm.ptr -> i32
%72 = arith.constant 1 : i32
%73 = arith.addi %71, %72 : i32
llvm.store %73, %19 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%75 = llvm.load %16 : !llvm.ptr -> !llvm.ptr
func.call @free(%75) : (!llvm.ptr) -> ()
%76 = llvm.load %7 : !llvm.ptr -> i32
%77 = arith.constant 1 : i32
%78 = arith.addi %76, %77 : i32
llvm.store %78, %7 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
func.return
}
func.func @main() -> i32 {
%80 = arith.constant 40 : i32
%81 = arith.constant 8 : i32
%82 = arith.extsi %80 : i32 to i64
%83 = arith.extsi %81 : i32 to i64
%79 = func.call @calloc(%82, %83) : (i64, i64) -> !llvm.ptr
%85 = arith.constant 40 : i32
%86 = arith.constant 8 : i32
%87 = arith.extsi %85 : i32 to i64
%88 = arith.extsi %86 : i32 to i64
%84 = func.call @calloc(%87, %88) : (i64, i64) -> !llvm.ptr
%89 = llvm.mlir.zero : !llvm.ptr
%90 = llvm.icmp "eq" %79, %89 : !llvm.ptr
%91 = scf.if %90 -> (i1) {
%92 = arith.constant true
scf.yield %92 : i1
} else {
%93 = llvm.mlir.zero : !llvm.ptr
%94 = llvm.icmp "eq" %84, %93 : !llvm.ptr
scf.yield %94 : i1
}
cf.cond_br %91, ^bb15, ^bb16
^bb15:
%95 = arith.constant 1 : i32
func.return %95 : i32
^bb16:
cf.br ^bb17
^bb17:
%97 = arith.constant 9 : i32
%98 = arith.constant 4 : i32
func.call @roll_dp(%97, %98, %79) : (i32, i32, !llvm.ptr) -> ()
%100 = arith.constant 6 : i32
%101 = arith.constant 6 : i32
func.call @roll_dp(%100, %101, %84) : (i32, i32, !llvm.ptr) -> ()
%102 = arith.constant 0.0 : f32
%103 = arith.extf %102 : f32 to f64
%104 = llvm.mlir.constant(1 : i64) : i64
%105 = llvm.alloca %104 x f64 : (i64) -> !llvm.ptr
llvm.store %103, %105 : f64, !llvm.ptr
%106 = arith.constant 0.0 : f32
%107 = arith.extf %106 : f32 to f64
%108 = llvm.mlir.constant(1 : i64) : i64
%109 = llvm.alloca %108 x f64 : (i64) -> !llvm.ptr
llvm.store %107, %109 : f64, !llvm.ptr
%110 = arith.constant 0 : i32
%111 = llvm.mlir.constant(1 : i64) : i64
%112 = llvm.alloca %111 x i32 : (i64) -> !llvm.ptr
llvm.store %110, %112 : i32, !llvm.ptr
cf.br ^bb18
^bb18:
%113 = llvm.load %112 : !llvm.ptr -> i32
%114 = arith.constant 36 : i32
%115 = arith.cmpi sle, %113, %114 : i32
cf.cond_br %115, ^bb19, ^bb20
^bb19:
%116 = llvm.load %105 : !llvm.ptr -> f64
%118 = llvm.load %112 : !llvm.ptr -> i32
%119 = arith.extsi %118 : i32 to i64
%120 = llvm.getelementptr %79[%119] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%117 = llvm.load %120 : !llvm.ptr -> i64
%121 = arith.sitofp %117 : i64 to f64
%122 = arith.addf %116, %121 : f64
llvm.store %122, %105 : f64, !llvm.ptr
%123 = llvm.load %109 : !llvm.ptr -> f64
%125 = llvm.load %112 : !llvm.ptr -> i32
%126 = arith.extsi %125 : i32 to i64
%127 = llvm.getelementptr %84[%126] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%124 = llvm.load %127 : !llvm.ptr -> i64
%128 = arith.sitofp %124 : i64 to f64
%129 = arith.addf %123, %128 : f64
llvm.store %129, %109 : f64, !llvm.ptr
%130 = llvm.load %112 : !llvm.ptr -> i32
%131 = arith.constant 1 : i32
%132 = arith.addi %130, %131 : i32
llvm.store %132, %112 : i32, !llvm.ptr
cf.br ^bb18
^bb20:
%133 = arith.constant 0.0 : f32
%134 = arith.extf %133 : f32 to f64
%135 = llvm.mlir.constant(1 : i64) : i64
%136 = llvm.alloca %135 x f64 : (i64) -> !llvm.ptr
llvm.store %134, %136 : f64, !llvm.ptr
%137 = arith.constant 1 : i32
llvm.store %137, %112 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%138 = llvm.load %112 : !llvm.ptr -> i32
%139 = arith.constant 36 : i32
%140 = arith.cmpi sle, %138, %139 : i32
cf.cond_br %140, ^bb22, ^bb23
^bb22:
%141 = arith.constant 0.0 : f32
%142 = arith.extf %141 : f32 to f64
%143 = llvm.mlir.constant(1 : i64) : i64
%144 = llvm.alloca %143 x f64 : (i64) -> !llvm.ptr
llvm.store %142, %144 : f64, !llvm.ptr
%145 = arith.constant 1 : i32
%146 = llvm.mlir.constant(1 : i64) : i64
%147 = llvm.alloca %146 x i32 : (i64) -> !llvm.ptr
llvm.store %145, %147 : i32, !llvm.ptr
cf.br ^bb24
^bb24:
%148 = llvm.load %147 : !llvm.ptr -> i32
%149 = llvm.load %112 : !llvm.ptr -> i32
%150 = arith.cmpi slt, %148, %149 : i32
cf.cond_br %150, ^bb25, ^bb26
^bb25:
%151 = llvm.load %144 : !llvm.ptr -> f64
%153 = llvm.load %147 : !llvm.ptr -> i32
%154 = arith.extsi %153 : i32 to i64
%155 = llvm.getelementptr %84[%154] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%152 = llvm.load %155 : !llvm.ptr -> i64
%156 = arith.sitofp %152 : i64 to f64
%157 = arith.addf %151, %156 : f64
llvm.store %157, %144 : f64, !llvm.ptr
%158 = llvm.load %147 : !llvm.ptr -> i32
%159 = arith.constant 1 : i32
%160 = arith.addi %158, %159 : i32
llvm.store %160, %147 : i32, !llvm.ptr
cf.br ^bb24
^bb26:
%161 = llvm.load %136 : !llvm.ptr -> f64
%162 = llvm.load %144 : !llvm.ptr -> f64
%163 = llvm.load %109 : !llvm.ptr -> f64
%164 = arith.divf %162, %163 : f64
%166 = llvm.load %112 : !llvm.ptr -> i32
%167 = arith.extsi %166 : i32 to i64
%168 = llvm.getelementptr %79[%167] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%165 = llvm.load %168 : !llvm.ptr -> i64
%169 = arith.sitofp %165 : i64 to f64
%170 = llvm.load %105 : !llvm.ptr -> f64
%171 = arith.divf %169, %170 : f64
%172 = arith.mulf %164, %171 : f64
%173 = arith.addf %161, %172 : f64
llvm.store %173, %136 : f64, !llvm.ptr
%174 = llvm.load %112 : !llvm.ptr -> i32
%175 = arith.constant 1 : i32
%176 = arith.addi %174, %175 : i32
llvm.store %176, %112 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%177 = llvm.mlir.addressof @str_0 : !llvm.ptr
%178 = llvm.load %136 : !llvm.ptr -> f64
%179 = llvm.call @printf(%177, %178) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
func.call @free(%79) : (!llvm.ptr) -> ()
func.call @free(%84) : (!llvm.ptr) -> ()
%182 = arith.constant 0 : i32
func.return %182 : i32
}
}