Problem 081

Minimal path sum in matrix moving only right and down.

Answer427337
Output427337
StatusPASS
Native helperno
Runtime0 ms
Peak memory1168 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2)
Space complexityO(n)O(n^2)
ApproachFlow solutionBottom-up DP
VerdictOptimal

Flow source

# Project Euler 081
# Minimal path sum in matrix moving only right and down.

extern {
    function fopen(path: string, mode: string) -> ptr<void>
    function fgetc(f: ptr<void>) -> i32
    function fclose(f: ptr<void>) -> i32
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function read_int(f: ptr<void>) -> i32 {
    let mut c: i32 = fgetc(f)
    while c >= 0 && (c < 48 || c > 57) {
        c = fgetc(f)
    }
    if c < 0 { return -1 }
    let mut v: i32 = 0
    while c >= 48 && c <= 57 {
        v = v * 10 + (c - 48)
        c = fgetc(f)
    }
    return v
}

function min2(a: i64, b: i64) -> i64 {
    if a < b { return a }
    return b
}

function main() -> i32 {
    let n: i32 = 80
    let a: ptr<i64> = calloc((n * n) as i64, 8)
    if a == null { return 1 }
    let f: ptr<void> = fopen("data/p081.txt", "r")
    if f == null {
        printf("failed to read data/p081.txt\n")
        free(a)
        return 1
    }
    let mut i: i32 = 0
    while i < n * n {
        a[i] = read_int(f) as i64
        i = i + 1
    }
    fclose(f)

    let mut r: i32 = 0
    while r < n {
        let mut c: i32 = 0
        while c < n {
            if r == 0 && c == 0 {
                # start
            } elif r == 0 {
                a[c] = a[c] + a[c - 1]
            } elif c == 0 {
                a[r * n] = a[r * n] + a[(r - 1) * n]
            } else {
                a[r * n + c] = a[r * n + c] + min2(a[(r - 1) * n + c], a[r * n + c - 1])
            }
            c = c + 1
        }
        r = r + 1
    }
    printf("%lld\n", a[n * n - 1])
    free(a)
    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 read_int_ptr_void(void* f);
int64_t min2_i64_i64(int64_t a, int64_t b);
int32_t main(void);






int32_t read_int_ptr_void(void* f) {
    int32_t c = fgetc(f);
    while ((c >= 0 && (c < 48 || c > 57))) {
        c = fgetc(f);
    }
    if (c < 0) {
        return (-1);
    }
    int32_t v = 0;
    while ((c >= 48 && c <= 57)) {
        v = ((v * 10) + (c - 48));
        c = fgetc(f);
    }
    return v;
}

int64_t min2_i64_i64(int64_t a, int64_t b) {
    if (a < b) {
        return a;
    }
    return b;
}

int32_t main(void) {
    int32_t n = 80;
    int64_t* a = (int64_t*)(calloc(((int64_t)((n * n))), 8));
    if (a == NULL) {
        return 1;
    }
    void* f = (void*)(fopen("data/p081.txt", "r"));
    if (f == NULL) {
        printf("failed to read data/p081.txt\n");
        free(a);
        return 1;
    }
    int32_t i = 0;
    while (i < (n * n)) {
        a[i] = ((int64_t)(read_int_ptr_void(f)));
        i = (i + 1);
    }
    fclose(f);
    int32_t r = 0;
    while (r < n) {
        int32_t c = 0;
        while (c < n) {
            if ((r == 0 && c == 0)) {
            } else if (r == 0) {
                a[c] = (a[c] + a[(c - 1)]);
            } else if (c == 0) {
                a[(r * n)] = (a[(r * n)] + a[((r - 1) * n)]);
            } else {
                a[((r * n) + c)] = (a[((r * n) + c)] + min2_i64_i64(a[(((r - 1) * n) + c)], a[(((r * n) + c) - 1)]));
            }
            c = (c + 1);
        }
        r = (r + 1);
    }
    printf("%lld\n", a[((n * n) - 1)]);
    free(a);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("data/p081.txt\00") {addr_space = 0 : i32} : !llvm.array<14 x i8>
  llvm.mlir.global internal constant @str_1("r\00") {addr_space = 0 : i32} : !llvm.array<2 x i8>
  llvm.mlir.global internal constant @str_2("failed to read data/p081.txt\n\00") {addr_space = 0 : i32} : !llvm.array<30 x i8>
  llvm.mlir.global internal constant @str_3("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @fopen(!llvm.ptr, !llvm.ptr) -> !llvm.ptr
  func.func private @fgetc(!llvm.ptr) -> i32
  func.func private @fclose(!llvm.ptr) -> i32
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @read_int(%arg0: !llvm.ptr) -> i32 {
    %0 = func.call @fgetc(%arg0) : (!llvm.ptr) -> i32
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i32 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %3 = llvm.load %2 : !llvm.ptr -> i32
    %4 = arith.constant 0 : i32
    %5 = arith.cmpi sge, %3, %4 : i32
    %6 = scf.if %5 -> (i1) {
      %7 = llvm.load %2 : !llvm.ptr -> i32
      %8 = arith.constant 48 : i32
      %9 = arith.cmpi slt, %7, %8 : i32
      %10 = scf.if %9 -> (i1) {
        %11 = arith.constant true
        scf.yield %11 : i1
      } else {
        %12 = llvm.load %2 : !llvm.ptr -> i32
        %13 = arith.constant 57 : i32
        %14 = arith.cmpi sgt, %12, %13 : i32
        scf.yield %14 : i1
      }
      scf.yield %10 : i1
    } else {
      %15 = arith.constant false
      scf.yield %15 : i1
    }
    cf.cond_br %6, ^bb1, ^bb2
    ^bb1:
      %16 = func.call @fgetc(%arg0) : (!llvm.ptr) -> i32
      llvm.store %16, %2 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %17 = llvm.load %2 : !llvm.ptr -> i32
    %18 = arith.constant 0 : i32
    %19 = arith.cmpi slt, %17, %18 : i32
    cf.cond_br %19, ^bb3, ^bb4
    ^bb3:
      %20 = arith.constant 1 : i32
      %22 = arith.constant 0 : i32
      %21 = arith.subi %22, %20 : i32
      func.return %21 : i32
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %23 = arith.constant 0 : i32
    %24 = llvm.mlir.constant(1 : i64) : i64
    %25 = llvm.alloca %24 x i32 : (i64) -> !llvm.ptr
    llvm.store %23, %25 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %26 = llvm.load %2 : !llvm.ptr -> i32
    %27 = arith.constant 48 : i32
    %28 = arith.cmpi sge, %26, %27 : i32
    %29 = scf.if %28 -> (i1) {
      %30 = llvm.load %2 : !llvm.ptr -> i32
      %31 = arith.constant 57 : i32
      %32 = arith.cmpi sle, %30, %31 : i32
      scf.yield %32 : i1
    } else {
      %33 = arith.constant false
      scf.yield %33 : i1
    }
    cf.cond_br %29, ^bb7, ^bb8
    ^bb7:
      %34 = llvm.load %25 : !llvm.ptr -> i32
      %35 = arith.constant 10 : i32
      %36 = arith.muli %34, %35 : i32
      %37 = llvm.load %2 : !llvm.ptr -> i32
      %38 = arith.constant 48 : i32
      %39 = arith.subi %37, %38 : i32
      %40 = arith.addi %36, %39 : i32
      llvm.store %40, %25 : i32, !llvm.ptr
      %41 = func.call @fgetc(%arg0) : (!llvm.ptr) -> i32
      llvm.store %41, %2 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %42 = llvm.load %25 : !llvm.ptr -> i32
    func.return %42 : i32
  }
  func.func @min2(%arg0: i64, %arg1: i64) -> i64 {
    %43 = arith.cmpi slt, %arg0, %arg1 : i64
    cf.cond_br %43, ^bb9, ^bb10
    ^bb9:
      func.return %arg0 : i64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    func.return %arg1 : i64
  }
  func.func @main() -> i32 {
    %44 = arith.constant 80 : i32
    %46 = arith.muli %44, %44 : i32
    %47 = arith.extsi %46 : i32 to i64
    %48 = arith.constant 8 : i32
    %49 = arith.extsi %48 : i32 to i64
    %45 = func.call @calloc(%47, %49) : (i64, i64) -> !llvm.ptr
    %50 = llvm.mlir.zero : !llvm.ptr
    %51 = llvm.icmp "eq" %45, %50 : !llvm.ptr
    cf.cond_br %51, ^bb12, ^bb13
    ^bb12:
      %52 = arith.constant 1 : i32
      func.return %52 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %54 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %55 = llvm.mlir.addressof @str_1 : !llvm.ptr
    %53 = func.call @fopen(%54, %55) : (!llvm.ptr, !llvm.ptr) -> !llvm.ptr
    %56 = llvm.mlir.zero : !llvm.ptr
    %57 = llvm.icmp "eq" %53, %56 : !llvm.ptr
    cf.cond_br %57, ^bb15, ^bb16
    ^bb15:
      %58 = llvm.mlir.addressof @str_2 : !llvm.ptr
      %59 = llvm.call @printf(%58) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
      func.call @free(%45) : (!llvm.ptr) -> ()
      %61 = arith.constant 1 : i32
      func.return %61 : i32
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %62 = arith.constant 0 : i32
    %63 = llvm.mlir.constant(1 : i64) : i64
    %64 = llvm.alloca %63 x i32 : (i64) -> !llvm.ptr
    llvm.store %62, %64 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %65 = llvm.load %64 : !llvm.ptr -> i32
    %66 = arith.muli %44, %44 : i32
    %67 = arith.cmpi slt, %65, %66 : i32
    cf.cond_br %67, ^bb19, ^bb20
    ^bb19:
      %68 = func.call @read_int(%53) : (!llvm.ptr) -> i32
      %69 = arith.extsi %68 : i32 to i64
      %70 = llvm.load %64 : !llvm.ptr -> i32
      %71 = arith.extsi %70 : i32 to i64
      %72 = llvm.getelementptr %45[%71] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %69, %72 : i64, !llvm.ptr
      %73 = llvm.load %64 : !llvm.ptr -> i32
      %74 = arith.constant 1 : i32
      %75 = arith.addi %73, %74 : i32
      llvm.store %75, %64 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %76 = func.call @fclose(%53) : (!llvm.ptr) -> i32
    %77 = arith.constant 0 : i32
    %78 = llvm.mlir.constant(1 : i64) : i64
    %79 = llvm.alloca %78 x i32 : (i64) -> !llvm.ptr
    llvm.store %77, %79 : i32, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %80 = llvm.load %79 : !llvm.ptr -> i32
    %81 = arith.cmpi slt, %80, %44 : i32
    cf.cond_br %81, ^bb22, ^bb23
    ^bb22:
      %82 = arith.constant 0 : i32
      %83 = llvm.mlir.constant(1 : i64) : i64
      %84 = llvm.alloca %83 x i32 : (i64) -> !llvm.ptr
      llvm.store %82, %84 : i32, !llvm.ptr
      cf.br ^bb24
      ^bb24:
      %85 = llvm.load %84 : !llvm.ptr -> i32
      %86 = arith.cmpi slt, %85, %44 : i32
      cf.cond_br %86, ^bb25, ^bb26
      ^bb25:
        %87 = llvm.load %79 : !llvm.ptr -> i32
        %88 = arith.constant 0 : i32
        %89 = arith.cmpi eq, %87, %88 : i32
        %90 = scf.if %89 -> (i1) {
          %91 = llvm.load %84 : !llvm.ptr -> i32
          %92 = arith.constant 0 : i32
          %93 = arith.cmpi eq, %91, %92 : i32
          scf.yield %93 : i1
        } else {
          %94 = arith.constant false
          scf.yield %94 : i1
        }
        cf.cond_br %90, ^bb27, ^bb28
        ^bb27:
          cf.br ^bb29
        ^bb28:
          %95 = llvm.load %79 : !llvm.ptr -> i32
          %96 = arith.constant 0 : i32
          %97 = arith.cmpi eq, %95, %96 : i32
          cf.cond_br %97, ^bb31, ^bb30
        ^bb31:
          %99 = llvm.load %84 : !llvm.ptr -> i32
          %100 = arith.extsi %99 : i32 to i64
          %101 = llvm.getelementptr %45[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %98 = llvm.load %101 : !llvm.ptr -> i64
          %103 = llvm.load %84 : !llvm.ptr -> i32
          %104 = arith.constant 1 : i32
          %105 = arith.subi %103, %104 : i32
          %106 = arith.extsi %105 : i32 to i64
          %107 = llvm.getelementptr %45[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %102 = llvm.load %107 : !llvm.ptr -> i64
          %108 = arith.addi %98, %102 : i64
          %109 = llvm.load %84 : !llvm.ptr -> i32
          %110 = arith.extsi %109 : i32 to i64
          %111 = llvm.getelementptr %45[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %108, %111 : i64, !llvm.ptr
          cf.br ^bb29
        ^bb30:
          %112 = llvm.load %84 : !llvm.ptr -> i32
          %113 = arith.constant 0 : i32
          %114 = arith.cmpi eq, %112, %113 : i32
          cf.cond_br %114, ^bb33, ^bb32
        ^bb33:
          %116 = llvm.load %79 : !llvm.ptr -> i32
          %117 = arith.muli %116, %44 : i32
          %118 = arith.extsi %117 : i32 to i64
          %119 = llvm.getelementptr %45[%118] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %115 = llvm.load %119 : !llvm.ptr -> i64
          %121 = llvm.load %79 : !llvm.ptr -> i32
          %122 = arith.constant 1 : i32
          %123 = arith.subi %121, %122 : i32
          %124 = arith.muli %123, %44 : i32
          %125 = arith.extsi %124 : i32 to i64
          %126 = llvm.getelementptr %45[%125] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %120 = llvm.load %126 : !llvm.ptr -> i64
          %127 = arith.addi %115, %120 : i64
          %128 = llvm.load %79 : !llvm.ptr -> i32
          %129 = arith.muli %128, %44 : i32
          %130 = arith.extsi %129 : i32 to i64
          %131 = llvm.getelementptr %45[%130] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %127, %131 : i64, !llvm.ptr
          cf.br ^bb29
        ^bb32:
          %133 = llvm.load %79 : !llvm.ptr -> i32
          %134 = arith.muli %133, %44 : i32
          %135 = llvm.load %84 : !llvm.ptr -> i32
          %136 = arith.addi %134, %135 : i32
          %137 = arith.extsi %136 : i32 to i64
          %138 = llvm.getelementptr %45[%137] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %132 = llvm.load %138 : !llvm.ptr -> i64
          %141 = llvm.load %79 : !llvm.ptr -> i32
          %142 = arith.constant 1 : i32
          %143 = arith.subi %141, %142 : i32
          %144 = arith.muli %143, %44 : i32
          %145 = llvm.load %84 : !llvm.ptr -> i32
          %146 = arith.addi %144, %145 : i32
          %147 = arith.extsi %146 : i32 to i64
          %148 = llvm.getelementptr %45[%147] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %140 = llvm.load %148 : !llvm.ptr -> i64
          %150 = llvm.load %79 : !llvm.ptr -> i32
          %151 = arith.muli %150, %44 : i32
          %152 = llvm.load %84 : !llvm.ptr -> i32
          %153 = arith.addi %151, %152 : i32
          %154 = arith.constant 1 : i32
          %155 = arith.subi %153, %154 : i32
          %156 = arith.extsi %155 : i32 to i64
          %157 = llvm.getelementptr %45[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %149 = llvm.load %157 : !llvm.ptr -> i64
          %139 = func.call @min2(%140, %149) : (i64, i64) -> i64
          %158 = arith.addi %132, %139 : i64
          %159 = llvm.load %79 : !llvm.ptr -> i32
          %160 = arith.muli %159, %44 : i32
          %161 = llvm.load %84 : !llvm.ptr -> i32
          %162 = arith.addi %160, %161 : i32
          %163 = arith.extsi %162 : i32 to i64
          %164 = llvm.getelementptr %45[%163] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %158, %164 : i64, !llvm.ptr
          cf.br ^bb29
        ^bb29:
        %165 = llvm.load %84 : !llvm.ptr -> i32
        %166 = arith.constant 1 : i32
        %167 = arith.addi %165, %166 : i32
        llvm.store %167, %84 : i32, !llvm.ptr
        cf.br ^bb24
      ^bb26:
      %168 = llvm.load %79 : !llvm.ptr -> i32
      %169 = arith.constant 1 : i32
      %170 = arith.addi %168, %169 : i32
      llvm.store %170, %79 : i32, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %171 = llvm.mlir.addressof @str_3 : !llvm.ptr
    %173 = arith.muli %44, %44 : i32
    %174 = arith.constant 1 : i32
    %175 = arith.subi %173, %174 : i32
    %176 = arith.extsi %175 : i32 to i64
    %177 = llvm.getelementptr %45[%176] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %172 = llvm.load %177 : !llvm.ptr -> i64
    %178 = llvm.call @printf(%171, %172) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%45) : (!llvm.ptr) -> ()
    %180 = arith.constant 0 : i32
    func.return %180 : i32
  }
}