<commit_before>#pragma once
#include "util.cpp"

#include <set>
#include <tuple>
#include <deque>

long id(int r, int c, int a) {
    return r + 10000 * c + 1000000 * a;
}

long idl(int r, int c, int a, int l) {
    return r + 10000 * c + 1000000 * a + 100000000 * l;
}

bool inbounds(int r, int c, Input& input) {
    return (0 <= r && r < input.r && 0 <= c && c < input.c);
}

int tr_r(int r, int c, int a, Input& input) {
    // cerr << "translating " << r << " " << c << " " << a << endl;
    return r + input.movement_r[r][c][a];
}
int tr_c(int r, int c, int a, Input& input) {
    // cerr << "translating " << r << " " << c << " " << a << endl;
    return (c + input.movement_c[r][c][a] + input.c) % input.c;
}

void consider(Input& input, int r, int c, int a, int l, int rn, int cn, int an, queue<int>& q_r, queue<int>& q_c, queue<int>& q_a, queue<int>& q_l, map<long,int>& prevr, map<long,int>& prevc, map<long,int>& preva) {
    if (inbounds(rn, cn, input)) {
        q_r.push(rn);
        q_c.push(cn);
        q_a.push(an);
        q_l.push(l - 1);
        long i2 = idl(rn, cn, an, l - 1);
        prevr[i2] = r;
        prevc[i2] = c;
        preva[i2] = a;
    }
}

bool cyclefrom(Input& input, int r, int c, int a, int maxlen, deque<int>& tail_rs, deque<int>& tail_cs, deque<int>& tail_as, deque<int>& cycle_rs, deque<int>& cycle_cs, deque<int>& cycle_as) {

    cerr << "Considering " << r << " " << c << " " << a << endl;

    queue<int> q_r, q_c, q_a, q_l;

    q_r.push(r);
    q_c.push(c);
    q_a.push(a);
    q_l.push(maxlen);

    set<long> seen;
    map<long, int> prevr, prevc, preva;

    while(!q_r.empty()) {

        int r = q_r.front();
        int c = q_c.front();
        int a = q_a.front();
        int l = q_l.front();
        q_r.pop();
        q_c.pop();
        q_a.pop();
        q_l.pop();


        // cerr << "p(" << r << "," << c << "," << a << "," << l << ")" << endl;

        long i = id(r, c, a);
        if (seen.find(i) == seen.end()) {
            seen.insert(i);
            // cerr << "seen: ";
            // for (int s : seen) {
            //     cerr << s << " ";
            // }
            // cerr << endl;
        } else {
            cerr << "cycle of len " << (maxlen - l) << endl;
            cerr << "cycle to " << "(" << r << "," << c << "," << a << ")" << endl;
            // Compute cycle
            long lid = idl(r, c, a, l);
            
            int prer = prevr[lid];
            int prec = prevc[lid];
            int prea = preva[lid];
            l++;
            lid = idl(prer, prec, prea, l);
            long newid = id(prer, prec, prea);
            cerr << "going via " << "(" << prer << "," << prec << "," << prea << ")" << endl;

            cycle_rs.push_front(prer);
            cycle_rs.push_front(prec);
            cycle_rs.push_front(prea);

            // find cycle
            while(newid != i) {
                prer = prevr[lid];
                prec = prevc[lid];
                prea = preva[lid];
                l++;
                lid = idl(prer, prec, prea, l);
                newid = id(prer, prec, prea);
                cerr << "going via " << "(" << prer << "," << prec << "," << prea << ")" << endl;

                cycle_rs.push_front(prer);
                cycle_rs.push_front(prec);
                cycle_rs.push_front(prea);

                if (l >= maxlen) {
                    cerr << "obviously broken. stopping" << endl;
                    return false;
                }<commit_msg>cycles<commit_after>#pragma once
#include "util.cpp"

#include <set>
#include <tuple>
#include <deque>

long id(int r, int c, int a) {
    return r + 10000 * c + 1000000 * a;
}

long idl(int r, int c, int a, int l) {
    return r + 10000 * c + 1000000 * a + 100000000 * l;
}

bool inbounds(int r, int c, Input& input) {
    return (0 <= r && r < input.r && 0 <= c && c < input.c);
}

int tr_r(int r, int c, int a, Input& input) {
    // cerr << "translating " << r << " " << c << " " << a << endl;
    return r + input.movement_r[r][c][a];
}
int tr_c(int r, int c, int a, Input& input) {
    // cerr << "translating " << r << " " << c << " " << a << endl;
    return (c + input.movement_c[r][c][a] + input.c) % input.c;
}

void consider(Input& input, int r, int c, int a, int l, int rn, int cn, int an, queue<int>& q_r, queue<int>& q_c, queue<int>& q_a, queue<int>& q_l, map<long,int>& prevr, map<long,int>& prevc, map<long,int>& preva) {
    if (inbounds(rn, cn, input)) {
        q_r.push(rn);
        q_c.push(cn);
        q_a.push(an);
        q_l.push(l - 1);
        long i2 = idl(rn, cn, an, l - 1);
        prevr[i2] = r;
        prevc[i2] = c;
        preva[i2] = a;
    }
}

bool cyclefrom(Input& input, int r, int c, int a, int maxlen, deque<int>& tail_rs, deque<int>& tail_cs, deque<int>& tail_as, deque<int>& cycle_rs, deque<int>& cycle_cs, deque<int>& cycle_as) {

    cerr << "Considering " << r << " " << c << " " << a << endl;

    queue<int> q_r, q_c, q_a, q_l;

    q_r.push(r);
    q_c.push(c);
    q_a.push(a);
    q_l.push(maxlen);

    set<long> seen;
    map<long, int> prevr, prevc, preva;

    while(!q_r.empty()) {

        int r = q_r.front();
        int c = q_c.front();
        int a = q_a.front();
        int l = q_l.front();
        q_r.pop();
        q_c.pop();
        q_a.pop();
        q_l.pop();


        // cerr << "p(" << r << "," << c << "," << a << "," << l << ")" << endl;

        long i = id(r, c, a);
        if (seen.find(i) == seen.end()) {
            seen.insert(i);
            // cerr << "seen: ";
            // for (int s : seen) {
            //     cerr << s << " ";
            // }
            // cerr << endl;
        } else {
            cerr << "cycle of len " << (maxlen - l) << endl;
            cerr << "cycle to " << "(" << r << "," << c << "," << a << ")" << endl;
            // Compute cycle
            long lid = idl(r, c, a, l);
            
            int prer = prevr[lid];
            int prec = prevc[lid];
            int prea = preva[lid];
            l++;
            lid = idl(prer, prec, prea, l);
            long newid = id(prer, prec, prea);
            cerr << "going via " << "(" << prer << "," << prec << "," << prea << ")" << endl;

            cycle_rs.push_front(prer);
            cycle_rs.push_front(prec);
            cycle_rs.push_front(prea);

            // find cycle
            while(newid != i) {
                prer = prevr[lid];
                prec = prevc[lid];
                prea = preva[lid];
                l++;
                lid = idl(prer, prec, prea, l);
                newid = id(prer, prec, prea);
                cerr << "going via " << "(" << prer << "," << prec << "," << prea << ")" << endl;

                cycle_rs.push_front(prer);
                cycle_rs.push_front(prec);
                cycle_rs.push_front(prea);

                if (l >= maxlen + 2) {
                    cerr << "obviously broken. stopping" << endl;
                    return false;
                }
{'fraction_non_alphanumeric': 0.1944087861931251, 'fraction_numerical': 0.01183853943802596, 'mean_word_length': 1.6213084112149532, 'pattern_counts': {'":': 0, '<': 317, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2843224', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2577, 'n_words': 657}