Problem 109

How many distinct ways to checkout with a score less than 100?

Answer38182
Output38182
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 109
# How many distinct ways to checkout with a score less than 100?

function main() -> i32 {
    # singles 1..20,25; doubles 2..40,50; triples 3..60
    let mut values: array<i32, 62>
    let mut types: array<i32, 62>  # 1=S,2=D,3=T
    let mut n: i32 = 0
    let mut i: i32 = 1
    while i <= 20 {
        values[n] = i
        types[n] = 1
        n = n + 1
        values[n] = 2 * i
        types[n] = 2
        n = n + 1
        values[n] = 3 * i
        types[n] = 3
        n = n + 1
        i = i + 1
    }
    values[n] = 25
    types[n] = 1
    n = n + 1
    values[n] = 50
    types[n] = 2
    n = n + 1

    let mut count: i64 = 0
    # checkout ends with a double
    let mut d: i32 = 0
    while d < n {
        if types[d] != 2 {
            d = d + 1
            continue
        }
        let checkout: i32 = values[d]
        if checkout < 100 {
            count = count + 1  # just the double
        }
        # one dart then double
        let mut a: i32 = 0
        while a < n {
            let s: i32 = values[a] + checkout
            if s < 100 {
                count = count + 1
            }
            a = a + 1
        }
        # two darts then double — unordered for first two (a <= b by index to avoid perm)
        a = 0
        while a < n {
            let mut b: i32 = a
            while b < n {
                let s2: i32 = values[a] + values[b] + checkout
                if s2 < 100 {
                    count = count + 1
                }
                b = b + 1
            }
            a = a + 1
        }
        d = d + 1
    }
    printf("%lld\n", count)
    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) {
    int32_t values[62];
    int32_t types[62];
    int32_t n = 0;
    int32_t i = 1;
    while (i <= 20) {
        values[n] = i;
        types[n] = 1;
        n = (n + 1);
        values[n] = (2 * i);
        types[n] = 2;
        n = (n + 1);
        values[n] = (3 * i);
        types[n] = 3;
        n = (n + 1);
        i = (i + 1);
    }
    values[n] = 25;
    types[n] = 1;
    n = (n + 1);
    values[n] = 50;
    types[n] = 2;
    n = (n + 1);
    int64_t count = 0;
    int32_t d = 0;
    while (d < n) {
        if ((((unsigned)(d) < 62) ? types[d] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(d), 62), flow_fault_handler("array index out of bounds"), types[0])) != 2) {
            d = (d + 1);
            continue;
        }
        int32_t checkout = (((unsigned)(d) < 62) ? values[d] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(d), 62), flow_fault_handler("array index out of bounds"), values[0]));
        if (checkout < 100) {
            count = (count + 1);
        }
        int32_t a = 0;
        while (a < n) {
            int32_t s = ((((unsigned)(a) < 62) ? values[a] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(a), 62), flow_fault_handler("array index out of bounds"), values[0])) + checkout);
            if (s < 100) {
                count = (count + 1);
            }
            a = (a + 1);
        }
        a = 0;
        while (a < n) {
            int32_t b = a;
            while (b < n) {
                int32_t s2 = (((((unsigned)(a) < 62) ? values[a] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(a), 62), flow_fault_handler("array index out of bounds"), values[0])) + (((unsigned)(b) < 62) ? values[b] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(b), 62), flow_fault_handler("array index out of bounds"), values[0]))) + checkout);
                if (s2 < 100) {
                    count = (count + 1);
                }
                b = (b + 1);
            }
            a = (a + 1);
        }
        d = (d + 1);
    }
    printf("%lld\n", count);
    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 @main() -> i32 {
    %0 = memref.alloca() {type = memref<62xi32>} : memref<62xi32>
    %1 = memref.alloca() {type = memref<62xi32>} : memref<62xi32>
    %2 = arith.constant 0 : i32
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i32 : (i64) -> !llvm.ptr
    llvm.store %2, %4 : i32, !llvm.ptr
    %5 = arith.constant 1 : 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.constant 20 : i32
    %10 = arith.cmpi sle, %8, %9 : i32
    cf.cond_br %10, ^bb1, ^bb2
    ^bb1:
      %11 = llvm.load %7 : !llvm.ptr -> i32
      %12 = llvm.load %4 : !llvm.ptr -> i32
      %13 = arith.index_cast %12 : i32 to index
      memref.store %11, %0[%13] : memref<62xi32>
      %14 = arith.constant 1 : i32
      %15 = llvm.load %4 : !llvm.ptr -> i32
      %16 = arith.index_cast %15 : i32 to index
      memref.store %14, %1[%16] : memref<62xi32>
      %17 = llvm.load %4 : !llvm.ptr -> i32
      %18 = arith.constant 1 : i32
      %19 = arith.addi %17, %18 : i32
      llvm.store %19, %4 : i32, !llvm.ptr
      %20 = arith.constant 2 : i32
      %21 = llvm.load %7 : !llvm.ptr -> i32
      %22 = arith.muli %20, %21 : i32
      %23 = llvm.load %4 : !llvm.ptr -> i32
      %24 = arith.index_cast %23 : i32 to index
      memref.store %22, %0[%24] : memref<62xi32>
      %25 = arith.constant 2 : i32
      %26 = llvm.load %4 : !llvm.ptr -> i32
      %27 = arith.index_cast %26 : i32 to index
      memref.store %25, %1[%27] : memref<62xi32>
      %28 = llvm.load %4 : !llvm.ptr -> i32
      %29 = arith.constant 1 : i32
      %30 = arith.addi %28, %29 : i32
      llvm.store %30, %4 : i32, !llvm.ptr
      %31 = arith.constant 3 : i32
      %32 = llvm.load %7 : !llvm.ptr -> i32
      %33 = arith.muli %31, %32 : i32
      %34 = llvm.load %4 : !llvm.ptr -> i32
      %35 = arith.index_cast %34 : i32 to index
      memref.store %33, %0[%35] : memref<62xi32>
      %36 = arith.constant 3 : i32
      %37 = llvm.load %4 : !llvm.ptr -> i32
      %38 = arith.index_cast %37 : i32 to index
      memref.store %36, %1[%38] : memref<62xi32>
      %39 = llvm.load %4 : !llvm.ptr -> i32
      %40 = arith.constant 1 : i32
      %41 = arith.addi %39, %40 : i32
      llvm.store %41, %4 : i32, !llvm.ptr
      %42 = llvm.load %7 : !llvm.ptr -> i32
      %43 = arith.constant 1 : i32
      %44 = arith.addi %42, %43 : i32
      llvm.store %44, %7 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %45 = arith.constant 25 : i32
    %46 = llvm.load %4 : !llvm.ptr -> i32
    %47 = arith.index_cast %46 : i32 to index
    memref.store %45, %0[%47] : memref<62xi32>
    %48 = arith.constant 1 : i32
    %49 = llvm.load %4 : !llvm.ptr -> i32
    %50 = arith.index_cast %49 : i32 to index
    memref.store %48, %1[%50] : memref<62xi32>
    %51 = llvm.load %4 : !llvm.ptr -> i32
    %52 = arith.constant 1 : i32
    %53 = arith.addi %51, %52 : i32
    llvm.store %53, %4 : i32, !llvm.ptr
    %54 = arith.constant 50 : i32
    %55 = llvm.load %4 : !llvm.ptr -> i32
    %56 = arith.index_cast %55 : i32 to index
    memref.store %54, %0[%56] : memref<62xi32>
    %57 = arith.constant 2 : i32
    %58 = llvm.load %4 : !llvm.ptr -> i32
    %59 = arith.index_cast %58 : i32 to index
    memref.store %57, %1[%59] : memref<62xi32>
    %60 = llvm.load %4 : !llvm.ptr -> i32
    %61 = arith.constant 1 : i32
    %62 = arith.addi %60, %61 : i32
    llvm.store %62, %4 : i32, !llvm.ptr
    %63 = arith.constant 0 : i32
    %64 = arith.extsi %63 : i32 to i64
    %65 = llvm.mlir.constant(1 : i64) : i64
    %66 = llvm.alloca %65 x i64 : (i64) -> !llvm.ptr
    llvm.store %64, %66 : i64, !llvm.ptr
    %67 = arith.constant 0 : i32
    %68 = llvm.mlir.constant(1 : i64) : i64
    %69 = llvm.alloca %68 x i32 : (i64) -> !llvm.ptr
    llvm.store %67, %69 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %70 = llvm.load %69 : !llvm.ptr -> i32
    %71 = llvm.load %4 : !llvm.ptr -> i32
    %72 = arith.cmpi slt, %70, %71 : i32
    cf.cond_br %72, ^bb4, ^bb5
    ^bb4:
      %74 = llvm.load %69 : !llvm.ptr -> i32
      %75 = arith.index_cast %74 : i32 to index
      %73 = memref.load %1[%75] : memref<62xi32>
      %76 = arith.constant 2 : i32
      %77 = arith.cmpi ne, %73, %76 : i32
      cf.cond_br %77, ^bb6, ^bb7
      ^bb6:
        %78 = llvm.load %69 : !llvm.ptr -> i32
        %79 = arith.constant 1 : i32
        %80 = arith.addi %78, %79 : i32
        llvm.store %80, %69 : i32, !llvm.ptr
        cf.br ^bb3
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %82 = llvm.load %69 : !llvm.ptr -> i32
      %83 = arith.index_cast %82 : i32 to index
      %81 = memref.load %0[%83] : memref<62xi32>
      %84 = arith.constant 100 : i32
      %85 = arith.cmpi slt, %81, %84 : i32
      cf.cond_br %85, ^bb9, ^bb10
      ^bb9:
        %86 = llvm.load %66 : !llvm.ptr -> i64
        %87 = arith.constant 1 : i32
        %89 = arith.extsi %87 : i32 to i64
        %88 = arith.addi %86, %89 : i64
        llvm.store %88, %66 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %90 = arith.constant 0 : i32
      %91 = llvm.mlir.constant(1 : i64) : i64
      %92 = llvm.alloca %91 x i32 : (i64) -> !llvm.ptr
      llvm.store %90, %92 : i32, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %93 = llvm.load %92 : !llvm.ptr -> i32
      %94 = llvm.load %4 : !llvm.ptr -> i32
      %95 = arith.cmpi slt, %93, %94 : i32
      cf.cond_br %95, ^bb13, ^bb14
      ^bb13:
        %97 = llvm.load %92 : !llvm.ptr -> i32
        %98 = arith.index_cast %97 : i32 to index
        %96 = memref.load %0[%98] : memref<62xi32>
        %99 = arith.addi %96, %81 : i32
        %100 = arith.constant 100 : i32
        %101 = arith.cmpi slt, %99, %100 : i32
        cf.cond_br %101, ^bb15, ^bb16
        ^bb15:
          %102 = llvm.load %66 : !llvm.ptr -> i64
          %103 = arith.constant 1 : i32
          %105 = arith.extsi %103 : i32 to i64
          %104 = arith.addi %102, %105 : i64
          llvm.store %104, %66 : i64, !llvm.ptr
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %106 = llvm.load %92 : !llvm.ptr -> i32
        %107 = arith.constant 1 : i32
        %108 = arith.addi %106, %107 : i32
        llvm.store %108, %92 : i32, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %109 = arith.constant 0 : i32
      llvm.store %109, %92 : i32, !llvm.ptr
      cf.br ^bb18
      ^bb18:
      %110 = llvm.load %92 : !llvm.ptr -> i32
      %111 = llvm.load %4 : !llvm.ptr -> i32
      %112 = arith.cmpi slt, %110, %111 : i32
      cf.cond_br %112, ^bb19, ^bb20
      ^bb19:
        %113 = llvm.load %92 : !llvm.ptr -> i32
        %114 = llvm.mlir.constant(1 : i64) : i64
        %115 = llvm.alloca %114 x i32 : (i64) -> !llvm.ptr
        llvm.store %113, %115 : i32, !llvm.ptr
        cf.br ^bb21
        ^bb21:
        %116 = llvm.load %115 : !llvm.ptr -> i32
        %117 = llvm.load %4 : !llvm.ptr -> i32
        %118 = arith.cmpi slt, %116, %117 : i32
        cf.cond_br %118, ^bb22, ^bb23
        ^bb22:
          %120 = llvm.load %92 : !llvm.ptr -> i32
          %121 = arith.index_cast %120 : i32 to index
          %119 = memref.load %0[%121] : memref<62xi32>
          %123 = llvm.load %115 : !llvm.ptr -> i32
          %124 = arith.index_cast %123 : i32 to index
          %122 = memref.load %0[%124] : memref<62xi32>
          %125 = arith.addi %119, %122 : i32
          %126 = arith.addi %125, %81 : i32
          %127 = arith.constant 100 : i32
          %128 = arith.cmpi slt, %126, %127 : i32
          cf.cond_br %128, ^bb24, ^bb25
          ^bb24:
            %129 = llvm.load %66 : !llvm.ptr -> i64
            %130 = arith.constant 1 : i32
            %132 = arith.extsi %130 : i32 to i64
            %131 = arith.addi %129, %132 : i64
            llvm.store %131, %66 : i64, !llvm.ptr
            cf.br ^bb26
          ^bb25:
            cf.br ^bb26
          ^bb26:
          %133 = llvm.load %115 : !llvm.ptr -> i32
          %134 = arith.constant 1 : i32
          %135 = arith.addi %133, %134 : i32
          llvm.store %135, %115 : i32, !llvm.ptr
          cf.br ^bb21
        ^bb23:
        %136 = llvm.load %92 : !llvm.ptr -> i32
        %137 = arith.constant 1 : i32
        %138 = arith.addi %136, %137 : i32
        llvm.store %138, %92 : i32, !llvm.ptr
        cf.br ^bb18
      ^bb20:
      %139 = llvm.load %69 : !llvm.ptr -> i32
      %140 = arith.constant 1 : i32
      %141 = arith.addi %139, %140 : i32
      llvm.store %141, %69 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %142 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %143 = llvm.load %66 : !llvm.ptr -> i64
    %144 = llvm.call @printf(%142, %143) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %145 = arith.constant 0 : i32
    func.return %145 : i32
  }
}