← All problems
Problem 018
Maximum path sum through the triangle (bottom-up DP).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n^2)
Space complexity O(n)O(n^2)
Approach Flow solution Bottom-up DP on triangle
Verdict Optimal
Flow source
# Project Euler 018
# Maximum path sum through the triangle (bottom-up DP).
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 max2(a: i64, b: i64) -> i64 {
if a > b {
return a
}
return b
}
function main() -> i32 {
let rows: i32 = 15
let cells: i32 = rows * (rows + 1) / 2
let t: ptr<i64> = calloc(cells as i64, 8)
if t == null {
return 1
}
let f: ptr<void> = fopen("data/p018.txt", "r")
if f == null {
printf("failed to read data/p018.txt\n")
free(t)
return 1
}
let mut idx: i32 = 0
while idx < cells {
let v: i32 = read_int(f)
if v < 0 {
break
}
t[idx] = v as i64
idx = idx + 1
}
fclose(f)
let mut r: i32 = rows - 2
while r >= 0 {
let base: i32 = r * (r + 1) / 2
let below: i32 = (r + 1) * (r + 2) / 2
let mut c: i32 = 0
while c <= r {
t[base + c] = t[base + c] + max2(t[below + c], t[below + c + 1])
c = c + 1
}
r = r - 1
}
printf("%lld\n", t[0])
free(t)
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 max2_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 max2_i64_i64(int64_t a, int64_t b) {
if (a > b) {
return a;
}
return b;
}
int32_t main(void) {
int32_t rows = 15;
int32_t cells = FLOW_CHECKED_DIV(((rows * (rows + 1))), (2));
int64_t* t = (int64_t*)(calloc(((int64_t)(cells)), 8));
if (t == NULL) {
return 1;
}
void* f = (void*)(fopen("data/p018.txt", "r"));
if (f == NULL) {
printf("failed to read data/p018.txt\n");
free(t);
return 1;
}
int32_t idx = 0;
while (idx < cells) {
int32_t v = read_int_ptr_void(f);
if (v < 0) {
break;
}
t[idx] = ((int64_t)(v));
idx = (idx + 1);
}
fclose(f);
int32_t r = (rows - 2);
while (r >= 0) {
int32_t base = FLOW_CHECKED_DIV(((r * (r + 1))), (2));
int32_t below = FLOW_CHECKED_DIV((((r + 1) * (r + 2))), (2));
int32_t c = 0;
while (c <= r) {
t[(base + c)] = (t[(base + c)] + max2_i64_i64(t[(below + c)], t[((below + c) + 1)]));
c = (c + 1);
}
r = (r - 1);
}
printf("%lld\n", t[0]);
free(t);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("data/p018.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/p018.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 @max2(%arg0: i64, %arg1: i64) -> i64 {
%43 = arith.cmpi sgt, %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 15 : i32
%45 = arith.constant 1 : i32
%46 = arith.addi %44, %45 : i32
%47 = arith.muli %44, %46 : i32
%48 = arith.constant 2 : i32
%49 = arith.divsi %47, %48 : i32
%51 = arith.extsi %49 : i32 to i64
%52 = arith.constant 8 : i32
%53 = arith.extsi %52 : i32 to i64
%50 = func.call @calloc(%51, %53) : (i64, i64) -> !llvm.ptr
%54 = llvm.mlir.zero : !llvm.ptr
%55 = llvm.icmp "eq" %50, %54 : !llvm.ptr
cf.cond_br %55, ^bb12, ^bb13
^bb12:
%56 = arith.constant 1 : i32
func.return %56 : i32
^bb13:
cf.br ^bb14
^bb14:
%58 = llvm.mlir.addressof @str_0 : !llvm.ptr
%59 = llvm.mlir.addressof @str_1 : !llvm.ptr
%57 = func.call @fopen(%58, %59) : (!llvm.ptr, !llvm.ptr) -> !llvm.ptr
%60 = llvm.mlir.zero : !llvm.ptr
%61 = llvm.icmp "eq" %57, %60 : !llvm.ptr
cf.cond_br %61, ^bb15, ^bb16
^bb15:
%62 = llvm.mlir.addressof @str_2 : !llvm.ptr
%63 = llvm.call @printf(%62) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
func.call @free(%50) : (!llvm.ptr) -> ()
%65 = arith.constant 1 : i32
func.return %65 : i32
^bb16:
cf.br ^bb17
^bb17:
%66 = arith.constant 0 : i32
%67 = llvm.mlir.constant(1 : i64) : i64
%68 = llvm.alloca %67 x i32 : (i64) -> !llvm.ptr
llvm.store %66, %68 : i32, !llvm.ptr
cf.br ^bb18
^bb18:
%69 = llvm.load %68 : !llvm.ptr -> i32
%70 = arith.cmpi slt, %69, %49 : i32
cf.cond_br %70, ^bb19, ^bb20
^bb19:
%71 = func.call @read_int(%57) : (!llvm.ptr) -> i32
%72 = arith.constant 0 : i32
%73 = arith.cmpi slt, %71, %72 : i32
cf.cond_br %73, ^bb21, ^bb22
^bb21:
cf.br ^bb20
^bb22:
cf.br ^bb23
^bb23:
%74 = arith.extsi %71 : i32 to i64
%75 = llvm.load %68 : !llvm.ptr -> i32
%76 = arith.extsi %75 : i32 to i64
%77 = llvm.getelementptr %50[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %74, %77 : i64, !llvm.ptr
%78 = llvm.load %68 : !llvm.ptr -> i32
%79 = arith.constant 1 : i32
%80 = arith.addi %78, %79 : i32
llvm.store %80, %68 : i32, !llvm.ptr
cf.br ^bb18
^bb20:
%81 = func.call @fclose(%57) : (!llvm.ptr) -> i32
%82 = arith.constant 2 : i32
%83 = arith.subi %44, %82 : i32
%84 = llvm.mlir.constant(1 : i64) : i64
%85 = llvm.alloca %84 x i32 : (i64) -> !llvm.ptr
llvm.store %83, %85 : i32, !llvm.ptr
cf.br ^bb24
^bb24:
%86 = llvm.load %85 : !llvm.ptr -> i32
%87 = arith.constant 0 : i32
%88 = arith.cmpi sge, %86, %87 : i32
cf.cond_br %88, ^bb25, ^bb26
^bb25:
%89 = llvm.load %85 : !llvm.ptr -> i32
%90 = llvm.load %85 : !llvm.ptr -> i32
%91 = arith.constant 1 : i32
%92 = arith.addi %90, %91 : i32
%93 = arith.muli %89, %92 : i32
%94 = arith.constant 2 : i32
%95 = arith.divsi %93, %94 : i32
%96 = llvm.load %85 : !llvm.ptr -> i32
%97 = arith.constant 1 : i32
%98 = arith.addi %96, %97 : i32
%99 = llvm.load %85 : !llvm.ptr -> i32
%100 = arith.constant 2 : i32
%101 = arith.addi %99, %100 : i32
%102 = arith.muli %98, %101 : i32
%103 = arith.constant 2 : i32
%104 = arith.divsi %102, %103 : i32
%105 = arith.constant 0 : i32
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x i32 : (i64) -> !llvm.ptr
llvm.store %105, %107 : i32, !llvm.ptr
cf.br ^bb27
^bb27:
%108 = llvm.load %107 : !llvm.ptr -> i32
%109 = llvm.load %85 : !llvm.ptr -> i32
%110 = arith.cmpi sle, %108, %109 : i32
cf.cond_br %110, ^bb28, ^bb29
^bb28:
%112 = llvm.load %107 : !llvm.ptr -> i32
%113 = arith.addi %95, %112 : i32
%114 = arith.extsi %113 : i32 to i64
%115 = llvm.getelementptr %50[%114] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%111 = llvm.load %115 : !llvm.ptr -> i64
%118 = llvm.load %107 : !llvm.ptr -> i32
%119 = arith.addi %104, %118 : i32
%120 = arith.extsi %119 : i32 to i64
%121 = llvm.getelementptr %50[%120] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%117 = llvm.load %121 : !llvm.ptr -> i64
%123 = llvm.load %107 : !llvm.ptr -> i32
%124 = arith.addi %104, %123 : i32
%125 = arith.constant 1 : i32
%126 = arith.addi %124, %125 : i32
%127 = arith.extsi %126 : i32 to i64
%128 = llvm.getelementptr %50[%127] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%122 = llvm.load %128 : !llvm.ptr -> i64
%116 = func.call @max2(%117, %122) : (i64, i64) -> i64
%129 = arith.addi %111, %116 : i64
%130 = llvm.load %107 : !llvm.ptr -> i32
%131 = arith.addi %95, %130 : i32
%132 = arith.extsi %131 : i32 to i64
%133 = llvm.getelementptr %50[%132] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %129, %133 : i64, !llvm.ptr
%134 = llvm.load %107 : !llvm.ptr -> i32
%135 = arith.constant 1 : i32
%136 = arith.addi %134, %135 : i32
llvm.store %136, %107 : i32, !llvm.ptr
cf.br ^bb27
^bb29:
%137 = llvm.load %85 : !llvm.ptr -> i32
%138 = arith.constant 1 : i32
%139 = arith.subi %137, %138 : i32
llvm.store %139, %85 : i32, !llvm.ptr
cf.br ^bb24
^bb26:
%140 = llvm.mlir.addressof @str_3 : !llvm.ptr
%142 = arith.constant 0 : i32
%143 = arith.extsi %142 : i32 to i64
%144 = llvm.getelementptr %50[%143] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%141 = llvm.load %144 : !llvm.ptr -> i64
%145 = llvm.call @printf(%140, %141) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%50) : (!llvm.ptr) -> ()
%147 = arith.constant 0 : i32
func.return %147 : i32
}
}