Problem 116

Ways to tile length 50 with black unit tiles and colored tiles of length 2,3, or 4 (exactly one color used per counting; sum the three cases).

Answer20492570929
Output20492570929
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 116
# Ways to tile length 50 with black unit tiles and colored tiles of length 2,3, or 4
# (exactly one color used per counting; sum the three cases).

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

function tile_ways(n: i32, tlen: i32) -> i64 {
    let a: ptr<i64> = calloc((n + 1) as i64, 8)
    if a == null { return 0 }
    a[0] = 1
    let mut i: i32 = 1
    while i <= n {
        a[i] = a[i - 1]
        if i >= tlen {
            a[i] = a[i] + a[i - tlen]
        }
        i = i + 1
    }
    let r: i64 = a[n] - 1  # exclude all-black
    free(a)
    return r
}

function main() -> i32 {
    let n: i32 = 50
    let ans: i64 = tile_ways(n, 2) + tile_ways(n, 3) + tile_ways(n, 4)
    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; }

int64_t tile_ways_i32_i32(int32_t n, int32_t tlen);
int32_t main(void);



int64_t tile_ways_i32_i32(int32_t n, int32_t tlen) {
    int64_t* a = (int64_t*)(calloc(((int64_t)((n + 1))), 8));
    if (a == NULL) {
        return 0;
    }
    a[0] = 1;
    int32_t i = 1;
    while (i <= n) {
        a[i] = a[(i - 1)];
        if (i >= tlen) {
            a[i] = (a[i] + a[(i - tlen)]);
        }
        i = (i + 1);
    }
    int64_t r = (a[n] - 1);
    free(a);
    return r;
}

int32_t main(void) {
    int32_t n = 50;
    int64_t ans = ((tile_ways_i32_i32(n, 2) + tile_ways_i32_i32(n, 3)) + tile_ways_i32_i32(n, 4));
    printf("%lld\n", ans);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\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 @tile_ways(%arg0: i32, %arg1: i32) -> i64 {
    %1 = arith.constant 1 : i32
    %2 = arith.addi %arg0, %1 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = arith.constant 8 : i32
    %5 = arith.extsi %4 : i32 to i64
    %0 = func.call @calloc(%3, %5) : (i64, i64) -> !llvm.ptr
    %6 = llvm.mlir.zero : !llvm.ptr
    %7 = llvm.icmp "eq" %0, %6 : !llvm.ptr
    cf.cond_br %7, ^bb0, ^bb1
    ^bb0:
      %8 = arith.constant 0 : i32
      %9 = arith.extsi %8 : i32 to i64
      func.return %9 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %10 = arith.constant 1 : i32
    %11 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %13 = arith.extsi %11 : i32 to i64
    %14 = llvm.getelementptr %0[%13] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %12, %14 : i64, !llvm.ptr
    %15 = arith.constant 1 : i32
    %16 = llvm.mlir.constant(1 : i64) : i64
    %17 = llvm.alloca %16 x i32 : (i64) -> !llvm.ptr
    llvm.store %15, %17 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %18 = llvm.load %17 : !llvm.ptr -> i32
    %19 = arith.cmpi sle, %18, %arg0 : i32
    cf.cond_br %19, ^bb4, ^bb5
    ^bb4:
      %21 = llvm.load %17 : !llvm.ptr -> i32
      %22 = arith.constant 1 : i32
      %23 = arith.subi %21, %22 : i32
      %24 = arith.extsi %23 : i32 to i64
      %25 = llvm.getelementptr %0[%24] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %20 = llvm.load %25 : !llvm.ptr -> i64
      %26 = llvm.load %17 : !llvm.ptr -> i32
      %27 = arith.extsi %26 : i32 to i64
      %28 = llvm.getelementptr %0[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %20, %28 : i64, !llvm.ptr
      %29 = llvm.load %17 : !llvm.ptr -> i32
      %30 = arith.cmpi sge, %29, %arg1 : i32
      cf.cond_br %30, ^bb6, ^bb7
      ^bb6:
        %32 = llvm.load %17 : !llvm.ptr -> i32
        %33 = arith.extsi %32 : i32 to i64
        %34 = llvm.getelementptr %0[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %31 = llvm.load %34 : !llvm.ptr -> i64
        %36 = llvm.load %17 : !llvm.ptr -> i32
        %37 = arith.subi %36, %arg1 : i32
        %38 = arith.extsi %37 : i32 to i64
        %39 = llvm.getelementptr %0[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %35 = llvm.load %39 : !llvm.ptr -> i64
        %40 = arith.addi %31, %35 : i64
        %41 = llvm.load %17 : !llvm.ptr -> i32
        %42 = arith.extsi %41 : i32 to i64
        %43 = llvm.getelementptr %0[%42] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %40, %43 : i64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %44 = llvm.load %17 : !llvm.ptr -> i32
      %45 = arith.constant 1 : i32
      %46 = arith.addi %44, %45 : i32
      llvm.store %46, %17 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %48 = arith.extsi %arg0 : i32 to i64
    %49 = llvm.getelementptr %0[%48] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %47 = llvm.load %49 : !llvm.ptr -> i64
    %50 = arith.constant 1 : i32
    %52 = arith.extsi %50 : i32 to i64
    %51 = arith.subi %47, %52 : i64
    func.call @free(%0) : (!llvm.ptr) -> ()
    func.return %51 : i64
  }
  func.func @main() -> i32 {
    %54 = arith.constant 50 : i32
    %56 = arith.constant 2 : i32
    %55 = func.call @tile_ways(%54, %56) : (i32, i32) -> i64
    %58 = arith.constant 3 : i32
    %57 = func.call @tile_ways(%54, %58) : (i32, i32) -> i64
    %59 = arith.addi %55, %57 : i64
    %61 = arith.constant 4 : i32
    %60 = func.call @tile_ways(%54, %61) : (i32, i32) -> i64
    %62 = arith.addi %59, %60 : i64
    %63 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %64 = llvm.call @printf(%63, %62) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %65 = arith.constant 0 : i32
    func.return %65 : i32
  }
}