<commit_before>var wormLength = 10;

function init(cube) {
    return {
        pos: xyz(4, 4, 4),
        dir: 'x',
        speed: 1,
        prev: {
            dirs: [],
            speeds: [],
            i: 0
        }
    };
}

function effect(cube, vars) {
    cube(vars.pos).on();

    // backtracking
    if(vars.prev.dirs[0] != wormLength) {
        var tmpPos = utils.clone(pos);

        for(var i = vars.prev.i - 1, j = 0; j < wormLength; i--, j++) {
            if(i == -1) i = wormLength - 1;

            tmpPos[vars.prev.dirs[i]] -= vars.prev.speeds[i];

            cube(tmpPos).on();
        }
    }

    // relies on the fact that it's a cube
    // not entirely fool proof but probably good enough
    var newPos = vars.pos[vars.dir] + vars.speed;
    if(newPos < 0 || newPos >= cube.x || math.randint(0, 10) > 7) {
        vars.dir = nextDir(vars.dir);
        vars.speed = -vars.speed;
    }

    vars.prev_dirs[vars.prev.dir.i] = vars.dir;
    vars.prev_speeds[vars.prev.dir.i] = vars.speed;

    if(vars.prev.dir.i < wormLength - 1) {
        vars.prev.dir.i++;
    }
    else {
        vars.prev.dir.i = 0;
    }

    vars.pos[vars.dir] += vars.speed;
}

function nextDir(dir) {
    return {
        x: 'y',
        y: 'z',
        z: 'x'
    }[dir];
}<commit_msg>Simplify finite worm<commit_after>function init(cube) {
    var segments = queue(10);
    segments.push(xyz(4, 4, 4));

    return {
        segments: segments,
        dir: 'x',
        speed: 1
    };
}

function effect(cube, vars) {
    var segment = segments.last();

    cube().off();
    cube(segments).on();

    // figure out next segment
    // relies on the fact that it's a cube
    // not entirely fool proof but probably good enough
    var newPos = vars.pos[vars.dir] + vars.speed;
    if(newPos < 0 || newPos >= cube.x || math.randint(0, 10) > 7) {
        vars.dir = nextDir(vars.dir);
        vars.speed = -vars.speed;
    }

    var newSegment = segment.clone();
    newSegment[vars.dir] += vars.speed;
    segments.push(newSegment);
}

function nextDir(dir) {
    return {
        x: 'y',
        y: 'z',
        z: 'x'
    }[dir];
}
{'fraction_non_alphanumeric': 0.16293183940242764, 'fraction_numerical': 0.014005602240896359, 'mean_word_length': 1.8160315374507228, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6584117', 'n_tokens_mistral': 811, 'n_tokens_neox': 764, 'n_words': 177}