Problem 349
Langton's ant: black cells after 10^18 steps (detect period 104).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | ? |
| Space complexity | O(n^2) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 349
# Langton's ant: black cells after 10^18 steps (detect period 104).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i64 = 1000000000000000000
let size: i64 = 128
let grid: ptr<i8> = calloc(size * size, 1)
if grid == null { return 1 }
let mut x: i64 = size / 2
let mut y: i64 = size / 2
let dx: ptr<i64> = calloc(4, 8)
let dy: ptr<i64> = calloc(4, 8)
dx[0] = 0; dx[1] = 1; dx[2] = 0; dx[3] = -1
dy[0] = 1; dy[1] = 0; dy[2] = -1; dy[3] = 0
let mut direction: i64 = 0
let cycle: i64 = 104
let remainder: i64 = limit % cycle
let mut count: i64 = 0
let mut last: i64 = 0
let last_deltas: ptr<i64> = calloc(64, 8)
let mut ldc: i64 = 1
last_deltas[0] = 0
let stop_if_same: i64 = 10
let mut steps: i64 = 0
while steps < limit {
if steps % cycle == remainder {
let diff: i64 = count - last
last_deltas[ldc] = diff
ldc = ldc + 1
last = count
if ldc >= stop_if_same {
let mut samesame: bool = true
let mut scan: i64 = ldc - stop_if_same
while scan < ldc {
if last_deltas[scan] != diff {
samesame = false
break
}
scan = scan + 1
}
if samesame {
let remaining: i64 = (limit - steps) / cycle
count = count + remaining * diff
break
}
}
}
let pos: i64 = y * size + x
if grid[pos] == 0 {
grid[pos] = 1
count = count + 1
direction = (direction + 1) % 4
} else {
grid[pos] = 0
count = count - 1
direction = (direction + 3) % 4
}
x = x + dx[direction]
y = y + dy[direction]
steps = steps + 1
}
printf("%lld\n", count)
free(grid); free(dx); free(dy); free(last_deltas)
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 limit = 1000000000000000000;
int64_t size = 128;
int8_t* grid = (int8_t*)(calloc((size * size), 1));
if (grid == NULL) {
return 1;
}
int64_t x = FLOW_CHECKED_DIV((size), (2));
int64_t y = FLOW_CHECKED_DIV((size), (2));
int64_t* dx = (int64_t*)(calloc(4, 8));
int64_t* dy = (int64_t*)(calloc(4, 8));
dx[0] = 0;
dx[1] = 1;
dx[2] = 0;
dx[3] = (-1);
dy[0] = 1;
dy[1] = 0;
dy[2] = (-1);
dy[3] = 0;
int64_t direction = 0;
int64_t cycle = 104;
int64_t remainder = FLOW_CHECKED_MOD((limit), (cycle));
int64_t count = 0;
int64_t last = 0;
int64_t* last_deltas = (int64_t*)(calloc(64, 8));
int64_t ldc = 1;
last_deltas[0] = 0;
int64_t stop_if_same = 10;
int64_t steps = 0;
while (steps < limit) {
if (FLOW_CHECKED_MOD((steps), (cycle)) == remainder) {
int64_t diff = (count - last);
last_deltas[ldc] = diff;
ldc = (ldc + 1);
last = count;
if (ldc >= stop_if_same) {
bool samesame = 1;
int64_t scan = (ldc - stop_if_same);
while (scan < ldc) {
if (last_deltas[scan] != diff) {
samesame = 0;
break;
}
scan = (scan + 1);
}
if (samesame) {
int64_t remaining = FLOW_CHECKED_DIV(((limit - steps)), (cycle));
count = (count + (remaining * diff));
break;
}
}
}
int64_t pos = ((y * size) + x);
if (grid[pos] == 0) {
grid[pos] = 1;
count = (count + 1);
direction = FLOW_CHECKED_MOD(((direction + 1)), (4));
} else {
grid[pos] = 0;
count = (count - 1);
direction = FLOW_CHECKED_MOD(((direction + 3)), (4));
}
x = (x + dx[direction]);
y = (y + dy[direction]);
steps = (steps + 1);
}
printf("%lld\n", count);
free(grid);
free(dx);
free(dy);
free(last_deltas);
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 @main() -> i32 {
%0 = arith.constant 999999995705032704 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 128 : i32
%3 = arith.extsi %2 : i32 to i64
%5 = arith.muli %3, %3 : i64
%6 = arith.constant 1 : i32
%7 = arith.extsi %6 : i32 to i64
%4 = func.call @calloc(%5, %7) : (i64, i64) -> !llvm.ptr
%8 = llvm.mlir.zero : !llvm.ptr
%9 = llvm.icmp "eq" %4, %8 : !llvm.ptr
cf.cond_br %9, ^bb0, ^bb1
^bb0:
%10 = arith.constant 1 : i32
func.return %10 : i32
^bb1:
cf.br ^bb2
^bb2:
%11 = arith.constant 2 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.divsi %3, %13 : i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %12, %15 : i64, !llvm.ptr
%16 = arith.constant 2 : i32
%18 = arith.extsi %16 : i32 to i64
%17 = arith.divsi %3, %18 : i64
%19 = llvm.mlir.constant(1 : i64) : i64
%20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
llvm.store %17, %20 : i64, !llvm.ptr
%22 = arith.constant 4 : i32
%23 = arith.constant 8 : i32
%24 = arith.extsi %22 : i32 to i64
%25 = arith.extsi %23 : i32 to i64
%21 = func.call @calloc(%24, %25) : (i64, i64) -> !llvm.ptr
%27 = arith.constant 4 : i32
%28 = arith.constant 8 : i32
%29 = arith.extsi %27 : i32 to i64
%30 = arith.extsi %28 : i32 to i64
%26 = func.call @calloc(%29, %30) : (i64, i64) -> !llvm.ptr
%31 = arith.constant 0 : i32
%32 = arith.constant 0 : i32
%33 = arith.extsi %31 : i32 to i64
%34 = arith.extsi %32 : i32 to i64
%35 = llvm.getelementptr %21[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %33, %35 : i64, !llvm.ptr
%36 = arith.constant 1 : i32
%37 = arith.constant 1 : i32
%38 = arith.extsi %36 : i32 to i64
%39 = arith.extsi %37 : i32 to i64
%40 = llvm.getelementptr %21[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %38, %40 : i64, !llvm.ptr
%41 = arith.constant 0 : i32
%42 = arith.constant 2 : i32
%43 = arith.extsi %41 : i32 to i64
%44 = arith.extsi %42 : i32 to i64
%45 = llvm.getelementptr %21[%44] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %43, %45 : i64, !llvm.ptr
%46 = arith.constant 1 : i32
%48 = arith.constant 0 : i32
%47 = arith.subi %48, %46 : i32
%49 = arith.constant 3 : i32
%50 = arith.extsi %47 : i32 to i64
%51 = arith.extsi %49 : i32 to i64
%52 = llvm.getelementptr %21[%51] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %50, %52 : i64, !llvm.ptr
%53 = arith.constant 1 : i32
%54 = arith.constant 0 : i32
%55 = arith.extsi %53 : i32 to i64
%56 = arith.extsi %54 : i32 to i64
%57 = llvm.getelementptr %26[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %55, %57 : i64, !llvm.ptr
%58 = arith.constant 0 : i32
%59 = arith.constant 1 : i32
%60 = arith.extsi %58 : i32 to i64
%61 = arith.extsi %59 : i32 to i64
%62 = llvm.getelementptr %26[%61] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %60, %62 : i64, !llvm.ptr
%63 = arith.constant 1 : i32
%65 = arith.constant 0 : i32
%64 = arith.subi %65, %63 : i32
%66 = arith.constant 2 : i32
%67 = arith.extsi %64 : i32 to i64
%68 = arith.extsi %66 : i32 to i64
%69 = llvm.getelementptr %26[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %67, %69 : i64, !llvm.ptr
%70 = arith.constant 0 : i32
%71 = arith.constant 3 : i32
%72 = arith.extsi %70 : i32 to i64
%73 = arith.extsi %71 : i32 to i64
%74 = llvm.getelementptr %26[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %72, %74 : i64, !llvm.ptr
%75 = arith.constant 0 : i32
%76 = arith.extsi %75 : i32 to i64
%77 = llvm.mlir.constant(1 : i64) : i64
%78 = llvm.alloca %77 x i64 : (i64) -> !llvm.ptr
llvm.store %76, %78 : i64, !llvm.ptr
%79 = arith.constant 104 : i32
%80 = arith.extsi %79 : i32 to i64
%81 = arith.remsi %1, %80 : i64
%82 = arith.constant 0 : i32
%83 = arith.extsi %82 : i32 to i64
%84 = llvm.mlir.constant(1 : i64) : i64
%85 = llvm.alloca %84 x i64 : (i64) -> !llvm.ptr
llvm.store %83, %85 : i64, !llvm.ptr
%86 = arith.constant 0 : i32
%87 = arith.extsi %86 : i32 to i64
%88 = llvm.mlir.constant(1 : i64) : i64
%89 = llvm.alloca %88 x i64 : (i64) -> !llvm.ptr
llvm.store %87, %89 : i64, !llvm.ptr
%91 = arith.constant 64 : i32
%92 = arith.constant 8 : i32
%93 = arith.extsi %91 : i32 to i64
%94 = arith.extsi %92 : i32 to i64
%90 = func.call @calloc(%93, %94) : (i64, i64) -> !llvm.ptr
%95 = arith.constant 1 : i32
%96 = arith.extsi %95 : i32 to i64
%97 = llvm.mlir.constant(1 : i64) : i64
%98 = llvm.alloca %97 x i64 : (i64) -> !llvm.ptr
llvm.store %96, %98 : i64, !llvm.ptr
%99 = arith.constant 0 : i32
%100 = arith.constant 0 : i32
%101 = arith.extsi %99 : i32 to i64
%102 = arith.extsi %100 : i32 to i64
%103 = llvm.getelementptr %90[%102] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %101, %103 : i64, !llvm.ptr
%104 = arith.constant 10 : i32
%105 = arith.extsi %104 : i32 to i64
%106 = arith.constant 0 : i32
%107 = arith.extsi %106 : i32 to i64
%108 = llvm.mlir.constant(1 : i64) : i64
%109 = llvm.alloca %108 x i64 : (i64) -> !llvm.ptr
llvm.store %107, %109 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%110 = llvm.load %109 : !llvm.ptr -> i64
%111 = arith.cmpi slt, %110, %1 : i64
cf.cond_br %111, ^bb4, ^bb5
^bb4:
%112 = llvm.load %109 : !llvm.ptr -> i64
%113 = arith.remsi %112, %80 : i64
%114 = arith.cmpi eq, %113, %81 : i64
cf.cond_br %114, ^bb6, ^bb7
^bb6:
%115 = llvm.load %85 : !llvm.ptr -> i64
%116 = llvm.load %89 : !llvm.ptr -> i64
%117 = arith.subi %115, %116 : i64
%118 = llvm.load %98 : !llvm.ptr -> i64
%119 = llvm.getelementptr %90[%118] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %117, %119 : i64, !llvm.ptr
%120 = llvm.load %98 : !llvm.ptr -> i64
%121 = arith.constant 1 : i32
%123 = arith.extsi %121 : i32 to i64
%122 = arith.addi %120, %123 : i64
llvm.store %122, %98 : i64, !llvm.ptr
%124 = llvm.load %85 : !llvm.ptr -> i64
llvm.store %124, %89 : i64, !llvm.ptr
%125 = llvm.load %98 : !llvm.ptr -> i64
%126 = arith.cmpi sge, %125, %105 : i64
cf.cond_br %126, ^bb9, ^bb10
^bb9:
%127 = arith.constant 1 : i1
%128 = llvm.mlir.constant(1 : i64) : i64
%129 = llvm.alloca %128 x i1 : (i64) -> !llvm.ptr
llvm.store %127, %129 : i1, !llvm.ptr
%130 = llvm.load %98 : !llvm.ptr -> i64
%131 = arith.subi %130, %105 : i64
%132 = llvm.mlir.constant(1 : i64) : i64
%133 = llvm.alloca %132 x i64 : (i64) -> !llvm.ptr
llvm.store %131, %133 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%134 = llvm.load %133 : !llvm.ptr -> i64
%135 = llvm.load %98 : !llvm.ptr -> i64
%136 = arith.cmpi slt, %134, %135 : i64
cf.cond_br %136, ^bb13, ^bb14
^bb13:
%138 = llvm.load %133 : !llvm.ptr -> i64
%139 = llvm.getelementptr %90[%138] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%137 = llvm.load %139 : !llvm.ptr -> i64
%140 = arith.cmpi ne, %137, %117 : i64
cf.cond_br %140, ^bb15, ^bb16
^bb15:
%141 = arith.constant 0 : i1
llvm.store %141, %129 : i1, !llvm.ptr
cf.br ^bb14
^bb16:
cf.br ^bb17
^bb17:
%142 = llvm.load %133 : !llvm.ptr -> i64
%143 = arith.constant 1 : i32
%145 = arith.extsi %143 : i32 to i64
%144 = arith.addi %142, %145 : i64
llvm.store %144, %133 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%146 = llvm.load %129 : !llvm.ptr -> i1
cf.cond_br %146, ^bb18, ^bb19
^bb18:
%147 = llvm.load %109 : !llvm.ptr -> i64
%148 = arith.subi %1, %147 : i64
%149 = arith.divsi %148, %80 : i64
%150 = llvm.load %85 : !llvm.ptr -> i64
%151 = arith.muli %149, %117 : i64
%152 = arith.addi %150, %151 : i64
llvm.store %152, %85 : i64, !llvm.ptr
cf.br ^bb5
^bb19:
cf.br ^bb20
^bb20:
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%153 = llvm.load %20 : !llvm.ptr -> i64
%154 = arith.muli %153, %3 : i64
%155 = llvm.load %15 : !llvm.ptr -> i64
%156 = arith.addi %154, %155 : i64
%158 = llvm.getelementptr %4[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%157 = llvm.load %158 : !llvm.ptr -> i8
%159 = arith.constant 0 : i32
%161 = arith.extsi %157 : i8 to i32
%160 = arith.cmpi eq, %161, %159 : i32
cf.cond_br %160, ^bb21, ^bb22
^bb21:
%162 = arith.constant 1 : i32
%163 = arith.trunci %162 : i32 to i8
%164 = llvm.getelementptr %4[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %163, %164 : i8, !llvm.ptr
%165 = llvm.load %85 : !llvm.ptr -> i64
%166 = arith.constant 1 : i32
%168 = arith.extsi %166 : i32 to i64
%167 = arith.addi %165, %168 : i64
llvm.store %167, %85 : i64, !llvm.ptr
%169 = llvm.load %78 : !llvm.ptr -> i64
%170 = arith.constant 1 : i32
%172 = arith.extsi %170 : i32 to i64
%171 = arith.addi %169, %172 : i64
%173 = arith.constant 4 : i32
%175 = arith.extsi %173 : i32 to i64
%174 = arith.remsi %171, %175 : i64
llvm.store %174, %78 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
%176 = arith.constant 0 : i32
%177 = arith.trunci %176 : i32 to i8
%178 = llvm.getelementptr %4[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %177, %178 : i8, !llvm.ptr
%179 = llvm.load %85 : !llvm.ptr -> i64
%180 = arith.constant 1 : i32
%182 = arith.extsi %180 : i32 to i64
%181 = arith.subi %179, %182 : i64
llvm.store %181, %85 : i64, !llvm.ptr
%183 = llvm.load %78 : !llvm.ptr -> i64
%184 = arith.constant 3 : i32
%186 = arith.extsi %184 : i32 to i64
%185 = arith.addi %183, %186 : i64
%187 = arith.constant 4 : i32
%189 = arith.extsi %187 : i32 to i64
%188 = arith.remsi %185, %189 : i64
llvm.store %188, %78 : i64, !llvm.ptr
cf.br ^bb23
^bb23:
%190 = llvm.load %15 : !llvm.ptr -> i64
%192 = llvm.load %78 : !llvm.ptr -> i64
%193 = llvm.getelementptr %21[%192] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%191 = llvm.load %193 : !llvm.ptr -> i64
%194 = arith.addi %190, %191 : i64
llvm.store %194, %15 : i64, !llvm.ptr
%195 = llvm.load %20 : !llvm.ptr -> i64
%197 = llvm.load %78 : !llvm.ptr -> i64
%198 = llvm.getelementptr %26[%197] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%196 = llvm.load %198 : !llvm.ptr -> i64
%199 = arith.addi %195, %196 : i64
llvm.store %199, %20 : i64, !llvm.ptr
%200 = llvm.load %109 : !llvm.ptr -> i64
%201 = arith.constant 1 : i32
%203 = arith.extsi %201 : i32 to i64
%202 = arith.addi %200, %203 : i64
llvm.store %202, %109 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%204 = llvm.mlir.addressof @str_0 : !llvm.ptr
%205 = llvm.load %85 : !llvm.ptr -> i64
%206 = llvm.call @printf(%204, %205) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%4) : (!llvm.ptr) -> ()
func.call @free(%21) : (!llvm.ptr) -> ()
func.call @free(%26) : (!llvm.ptr) -> ()
func.call @free(%90) : (!llvm.ptr) -> ()
%211 = arith.constant 0 : i32
func.return %211 : i32
}
}