Problem 640

Shut the Box: expected turns for Bob to flip all 12 cards face down under optimal play. State = bitmask of face-up cards. Each roll (x, y) lets Bob toggle card x, y, or x+y; the Bellman equation V(s) = 1 + E[min choice V(s')] is a stochastic shortest path problem, solved by value iteration from V = 0 (monotone convergence; the check game with two coins and four cards gives 5.673651 as stated).

Answer50.317928
Output50.317928
StatusPASS
Native helperno
Runtime160 ms
Peak memory1136 KB
Time complexityO(n^4) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^4)O(n * m)
Space complexityO(n)O(n)
ApproachFlow solutionDynamic programming or generating function
VerdictUnknown

Flow source

# Project Euler 640
# Shut the Box: expected turns for Bob to flip all 12 cards face down
# under optimal play.
#
# State = bitmask of face-up cards.  Each roll (x, y) lets Bob toggle card
# x, y, or x+y; the Bellman equation V(s) = 1 + E[min choice V(s')] is a
# stochastic shortest path problem, solved by value iteration from V = 0
# (monotone convergence; the check game with two coins and four cards
# gives 5.673651 as stated).

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function main() -> i32 {
    let N: i64 = 4096
    let V: ptr<f64> = calloc(N, 8)
    let Vn: ptr<f64> = calloc(N, 8)

    let mut it: i64 = 0
    let mut diff: f64 = 1.0
    while it < 20000 && diff > 0.0000000000001 {
        diff = 0.0
        let mut s: i64 = 1
        while s < N {
            let mut tot: f64 = 0.0
            let mut x: i64 = 1
            while x <= 6 {
                let mut y: i64 = 1
                while y <= 6 {
                    # choices: toggle card x, y, or x+y (cards 1..12)
                    let mut best: f64 = V[s ^ (1 << (x - 1))]
                    let vy: f64 = V[s ^ (1 << (y - 1))]
                    if vy < best {
                        best = vy
                    }
                    let vxy: f64 = V[s ^ (1 << (x + y - 1))]
                    if vxy < best {
                        best = vxy
                    }
                    tot = tot + best
                    y = y + 1
                }
                x = x + 1
            }
            Vn[s] = 1.0 + tot / 36.0
            s = s + 1
        }
        s = 1
        while s < N {
            let d: f64 = Vn[s] - V[s]
            let mut ad: f64 = d
            if ad < 0.0 {
                ad = 0.0 - ad
            }
            if ad > diff {
                diff = ad
            }
            V[s] = Vn[s]
            s = s + 1
        }
        it = it + 1
    }

    printf("%.6f\n", V[N - 1])
    free(V)
    free(Vn)
    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; }

int32_t main(void);



int32_t main(void) {
    int64_t N = 4096;
    double* V = (double*)(calloc(N, 8));
    double* Vn = (double*)(calloc(N, 8));
    int64_t it = 0;
    double diff = 1.0;
    while ((it < 20000 && diff > 0.0000000000001)) {
        diff = 0.0;
        int64_t s = 1;
        while (s < N) {
            double tot = 0.0;
            int64_t x = 1;
            while (x <= 6) {
                int64_t y = 1;
                while (y <= 6) {
                    double best = V[(s ^ FLOW_CHECKED_SHL((1), ((x - 1))))];
                    double vy = V[(s ^ FLOW_CHECKED_SHL((1), ((y - 1))))];
                    if (vy < best) {
                        best = vy;
                    }
                    double vxy = V[(s ^ FLOW_CHECKED_SHL((1), (((x + y) - 1))))];
                    if (vxy < best) {
                        best = vxy;
                    }
                    tot = (tot + best);
                    y = (y + 1);
                }
                x = (x + 1);
            }
            Vn[s] = (1.0 + (tot / 36.0));
            s = (s + 1);
        }
        s = 1;
        while (s < N) {
            double d = (Vn[s] - V[s]);
            double ad = d;
            if (ad < 0.0) {
                ad = (0.0 - ad);
            }
            if (ad > diff) {
                diff = ad;
            }
            V[s] = Vn[s];
            s = (s + 1);
        }
        it = (it + 1);
    }
    printf("%.6f\n", V[(N - 1)]);
    free(V);
    free(Vn);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.6f\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 @main() -> i32 {
    %0 = arith.constant 4096 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 8 : i32
    %4 = arith.extsi %3 : i32 to i64
    %2 = func.call @calloc(%1, %4) : (i64, i64) -> !llvm.ptr
    %6 = arith.constant 8 : i32
    %7 = arith.extsi %6 : i32 to i64
    %5 = func.call @calloc(%1, %7) : (i64, i64) -> !llvm.ptr
    %8 = arith.constant 0 : i32
    %9 = arith.extsi %8 : i32 to i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    %12 = arith.constant 1.0 : f32
    %13 = arith.extf %12 : f32 to f64
    %14 = llvm.mlir.constant(1 : i64) : i64
    %15 = llvm.alloca %14 x f64 : (i64) -> !llvm.ptr
    llvm.store %13, %15 : f64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %16 = llvm.load %11 : !llvm.ptr -> i64
    %17 = arith.constant 20000 : i32
    %19 = arith.extsi %17 : i32 to i64
    %18 = arith.cmpi slt, %16, %19 : i64
    %20 = scf.if %18 -> (i1) {
      %21 = llvm.load %15 : !llvm.ptr -> f64
      %22 = arith.constant 0.0000000000001 : f32
      %24 = arith.extf %22 : f32 to f64
      %23 = arith.cmpf ogt, %21, %24 : f64
      scf.yield %23 : i1
    } else {
      %25 = arith.constant false
      scf.yield %25 : i1
    }
    cf.cond_br %20, ^bb1, ^bb2
    ^bb1:
      %26 = arith.constant 0.0 : f32
      %27 = arith.extf %26 : f32 to f64
      llvm.store %27, %15 : f64, !llvm.ptr
      %28 = arith.constant 1 : i32
      %29 = arith.extsi %28 : i32 to i64
      %30 = llvm.mlir.constant(1 : i64) : i64
      %31 = llvm.alloca %30 x i64 : (i64) -> !llvm.ptr
      llvm.store %29, %31 : i64, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %32 = llvm.load %31 : !llvm.ptr -> i64
      %33 = arith.cmpi slt, %32, %1 : i64
      cf.cond_br %33, ^bb4, ^bb5
      ^bb4:
        %34 = arith.constant 0.0 : f32
        %35 = arith.extf %34 : f32 to f64
        %36 = llvm.mlir.constant(1 : i64) : i64
        %37 = llvm.alloca %36 x f64 : (i64) -> !llvm.ptr
        llvm.store %35, %37 : f64, !llvm.ptr
        %38 = arith.constant 1 : i32
        %39 = arith.extsi %38 : i32 to i64
        %40 = llvm.mlir.constant(1 : i64) : i64
        %41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
        llvm.store %39, %41 : i64, !llvm.ptr
        cf.br ^bb6
        ^bb6:
        %42 = llvm.load %41 : !llvm.ptr -> i64
        %43 = arith.constant 6 : i32
        %45 = arith.extsi %43 : i32 to i64
        %44 = arith.cmpi sle, %42, %45 : i64
        cf.cond_br %44, ^bb7, ^bb8
        ^bb7:
          %46 = arith.constant 1 : i32
          %47 = arith.extsi %46 : i32 to i64
          %48 = llvm.mlir.constant(1 : i64) : i64
          %49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
          llvm.store %47, %49 : i64, !llvm.ptr
          cf.br ^bb9
          ^bb9:
          %50 = llvm.load %49 : !llvm.ptr -> i64
          %51 = arith.constant 6 : i32
          %53 = arith.extsi %51 : i32 to i64
          %52 = arith.cmpi sle, %50, %53 : i64
          cf.cond_br %52, ^bb10, ^bb11
          ^bb10:
            %55 = llvm.load %31 : !llvm.ptr -> i64
            %56 = arith.constant 1 : i32
            %57 = llvm.load %41 : !llvm.ptr -> i64
            %58 = arith.constant 1 : i32
            %60 = arith.extsi %58 : i32 to i64
            %59 = arith.subi %57, %60 : i64
            %62 = arith.extsi %56 : i32 to i64
            %61 = arith.shli %62, %59 : i64
            %63 = arith.xori %55, %61 : i64
            %64 = llvm.getelementptr %2[%63] : (!llvm.ptr, i64) -> !llvm.ptr, f64
            %54 = llvm.load %64 : !llvm.ptr -> f64
            %65 = llvm.mlir.constant(1 : i64) : i64
            %66 = llvm.alloca %65 x f64 : (i64) -> !llvm.ptr
            llvm.store %54, %66 : f64, !llvm.ptr
            %68 = llvm.load %31 : !llvm.ptr -> i64
            %69 = arith.constant 1 : i32
            %70 = llvm.load %49 : !llvm.ptr -> i64
            %71 = arith.constant 1 : i32
            %73 = arith.extsi %71 : i32 to i64
            %72 = arith.subi %70, %73 : i64
            %75 = arith.extsi %69 : i32 to i64
            %74 = arith.shli %75, %72 : i64
            %76 = arith.xori %68, %74 : i64
            %77 = llvm.getelementptr %2[%76] : (!llvm.ptr, i64) -> !llvm.ptr, f64
            %67 = llvm.load %77 : !llvm.ptr -> f64
            %78 = llvm.load %66 : !llvm.ptr -> f64
            %79 = arith.cmpf olt, %67, %78 : f64
            cf.cond_br %79, ^bb12, ^bb13
            ^bb12:
              llvm.store %67, %66 : f64, !llvm.ptr
              cf.br ^bb14
            ^bb13:
              cf.br ^bb14
            ^bb14:
            %81 = llvm.load %31 : !llvm.ptr -> i64
            %82 = arith.constant 1 : i32
            %83 = llvm.load %41 : !llvm.ptr -> i64
            %84 = llvm.load %49 : !llvm.ptr -> i64
            %85 = arith.addi %83, %84 : i64
            %86 = arith.constant 1 : i32
            %88 = arith.extsi %86 : i32 to i64
            %87 = arith.subi %85, %88 : i64
            %90 = arith.extsi %82 : i32 to i64
            %89 = arith.shli %90, %87 : i64
            %91 = arith.xori %81, %89 : i64
            %92 = llvm.getelementptr %2[%91] : (!llvm.ptr, i64) -> !llvm.ptr, f64
            %80 = llvm.load %92 : !llvm.ptr -> f64
            %93 = llvm.load %66 : !llvm.ptr -> f64
            %94 = arith.cmpf olt, %80, %93 : f64
            cf.cond_br %94, ^bb15, ^bb16
            ^bb15:
              llvm.store %80, %66 : f64, !llvm.ptr
              cf.br ^bb17
            ^bb16:
              cf.br ^bb17
            ^bb17:
            %95 = llvm.load %37 : !llvm.ptr -> f64
            %96 = llvm.load %66 : !llvm.ptr -> f64
            %97 = arith.addf %95, %96 : f64
            llvm.store %97, %37 : f64, !llvm.ptr
            %98 = llvm.load %49 : !llvm.ptr -> i64
            %99 = arith.constant 1 : i32
            %101 = arith.extsi %99 : i32 to i64
            %100 = arith.addi %98, %101 : i64
            llvm.store %100, %49 : i64, !llvm.ptr
            cf.br ^bb9
          ^bb11:
          %102 = llvm.load %41 : !llvm.ptr -> i64
          %103 = arith.constant 1 : i32
          %105 = arith.extsi %103 : i32 to i64
          %104 = arith.addi %102, %105 : i64
          llvm.store %104, %41 : i64, !llvm.ptr
          cf.br ^bb6
        ^bb8:
        %106 = arith.constant 1.0 : f32
        %107 = llvm.load %37 : !llvm.ptr -> f64
        %108 = arith.constant 36.0 : f32
        %110 = arith.extf %108 : f32 to f64
        %109 = arith.divf %107, %110 : f64
        %112 = arith.extf %106 : f32 to f64
        %111 = arith.addf %112, %109 : f64
        %113 = llvm.load %31 : !llvm.ptr -> i64
        %114 = llvm.getelementptr %5[%113] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        llvm.store %111, %114 : f64, !llvm.ptr
        %115 = llvm.load %31 : !llvm.ptr -> i64
        %116 = arith.constant 1 : i32
        %118 = arith.extsi %116 : i32 to i64
        %117 = arith.addi %115, %118 : i64
        llvm.store %117, %31 : i64, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %119 = arith.constant 1 : i32
      %120 = arith.extsi %119 : i32 to i64
      llvm.store %120, %31 : i64, !llvm.ptr
      cf.br ^bb18
      ^bb18:
      %121 = llvm.load %31 : !llvm.ptr -> i64
      %122 = arith.cmpi slt, %121, %1 : i64
      cf.cond_br %122, ^bb19, ^bb20
      ^bb19:
        %124 = llvm.load %31 : !llvm.ptr -> i64
        %125 = llvm.getelementptr %5[%124] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %123 = llvm.load %125 : !llvm.ptr -> f64
        %127 = llvm.load %31 : !llvm.ptr -> i64
        %128 = llvm.getelementptr %2[%127] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %126 = llvm.load %128 : !llvm.ptr -> f64
        %129 = arith.subf %123, %126 : f64
        %130 = llvm.mlir.constant(1 : i64) : i64
        %131 = llvm.alloca %130 x f64 : (i64) -> !llvm.ptr
        llvm.store %129, %131 : f64, !llvm.ptr
        %132 = llvm.load %131 : !llvm.ptr -> f64
        %133 = arith.constant 0.0 : f32
        %135 = arith.extf %133 : f32 to f64
        %134 = arith.cmpf olt, %132, %135 : f64
        cf.cond_br %134, ^bb21, ^bb22
        ^bb21:
          %136 = arith.constant 0.0 : f32
          %137 = llvm.load %131 : !llvm.ptr -> f64
          %139 = arith.extf %136 : f32 to f64
          %138 = arith.subf %139, %137 : f64
          llvm.store %138, %131 : f64, !llvm.ptr
          cf.br ^bb23
        ^bb22:
          cf.br ^bb23
        ^bb23:
        %140 = llvm.load %131 : !llvm.ptr -> f64
        %141 = llvm.load %15 : !llvm.ptr -> f64
        %142 = arith.cmpf ogt, %140, %141 : f64
        cf.cond_br %142, ^bb24, ^bb25
        ^bb24:
          %143 = llvm.load %131 : !llvm.ptr -> f64
          llvm.store %143, %15 : f64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          cf.br ^bb26
        ^bb26:
        %145 = llvm.load %31 : !llvm.ptr -> i64
        %146 = llvm.getelementptr %5[%145] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %144 = llvm.load %146 : !llvm.ptr -> f64
        %147 = llvm.load %31 : !llvm.ptr -> i64
        %148 = llvm.getelementptr %2[%147] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        llvm.store %144, %148 : f64, !llvm.ptr
        %149 = llvm.load %31 : !llvm.ptr -> i64
        %150 = arith.constant 1 : i32
        %152 = arith.extsi %150 : i32 to i64
        %151 = arith.addi %149, %152 : i64
        llvm.store %151, %31 : i64, !llvm.ptr
        cf.br ^bb18
      ^bb20:
      %153 = llvm.load %11 : !llvm.ptr -> i64
      %154 = arith.constant 1 : i32
      %156 = arith.extsi %154 : i32 to i64
      %155 = arith.addi %153, %156 : i64
      llvm.store %155, %11 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %157 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %159 = arith.constant 1 : i32
    %161 = arith.extsi %159 : i32 to i64
    %160 = arith.subi %1, %161 : i64
    %162 = llvm.getelementptr %2[%160] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %158 = llvm.load %162 : !llvm.ptr -> f64
    %163 = llvm.call @printf(%157, %158) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    func.call @free(%5) : (!llvm.ptr) -> ()
    %166 = arith.constant 0 : i32
    func.return %166 : i32
  }
}