all stats

Proloy's stats

guessed the most

namecorrect guessesgames togetherratio

were guessed the most by

namecorrect guessesgames togetherratio

entries

round #57

submitted at
2 likes

guesses
comments 0

post a comment


cg57.py ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## Python Version 3.9.7
## There was no `golfing' tomfoolery involved.
## Forsake your place in   clementine  heaven.
## For eso-limes guessed a puzzle   is solved.
## Please keep handy the metaphorical  heaven
## (That  rose  out  of   Shakespeare's  hand)
## While d(r)iving into this  serpent's  land:
## https://www.gutenberg.org/cache/epub/100/pg100.txt


import random # We will be using random for **spIkiness** :D

def generate_markov_chain(data, len_ctx): # Self-explanatory.
    res = {}
    for i in range(len_ctx, len(data)):
        window=data[i-len_ctx:i]
        res.setdefault(window, {})
        res[window].setdefault(data[i], 0)
        res[window][data[i]] += 1

    for prev_chars, next_chars in res.items():
        res[prev_chars] = list(next_chars.items()), sum(next_chars.values())

    return res

def generate_next_ch(markov_chain): # Self-explanatory.
    prob, total = markov_chain
    c = random.randrange(total)
    for i, p in prob:
        if c < p: return i
        c-=p
    return

def generate_text(markov_chain, prompt, len_gen_text): # Self-explanatory.
    lookback = len(list(markov_chain.keys())[0])
    if len(prompt) < lookback: 
        return "Error: prompt not long enough"

    for i in range(len(prompt), len_gen_text+1):
        ctx = prompt[i-lookback:i]
        if ctx not in markov_chain:
            return f"Error: prompt '{ctx}' wasn't found *inside* Markov"

        c = generate_next_ch(markov_chain[ctx])
        if c is None: break
        prompt += c
    return prompt

with open("corpus.txt", mode="r", encoding="utf-8") as file:
    m=generate_markov_chain(file.read(), 5)
    file.close()

    while True:
        t=generate_text(m, input(), 2000)
        print(t)

round #56

submitted at
0 likes

guesses
comments 0

post a comment


lyric.c ASCII text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum {
    LIT,
    CAT,
    OR,
    NONE,
    END
} Tag;

typedef struct Ast Ast;

typedef union {
    char ch;
    Ast *children;
} Data;

struct Ast {
    Tag tag;
    Data data;
};

Ast create_lit(char c) {
    return (Ast){
        .tag = LIT,
        .data = 
            (Data){.ch = c}
        };
}

Ast create_seq(Ast* c, Tag tag) {
    return (Ast){
        .tag = tag,
        .data = 
            (Data){.children = c}
        };
}

void cleanup(Ast ast) {
    if (ast.tag != CAT || ast.tag != OR) return;
    
    Ast* children = ast.data.children;

    for (int i=0;children[i].tag != END; i++) {
        cleanup(children[i]);
    }

    free(ast.data.children);
}

void init(Ast *seq, int len, int cap) {
    Ast end = {END, (Data){ .children =  NULL}};
    for (int i=len; i < cap; i++) seq[i]=end;
}

void* expect_nonnull(void* ptr) {
    if (ptr == NULL) {
        fprintf(stderr, "Oh no, it looks like I got too hungry for yo computer... *smacks lips*\n");
        exit(EXIT_FAILURE);
    }
    return ptr;
}

int ensure_nuf_space(void** vec, int required_cap, int cap, int size) {
    while (required_cap >= cap) {
        cap*=2;
    }
    *vec = expect_nonnull(realloc(*vec, cap*size));
    return cap;
}

int parse(char*, Ast*);

int parse_next(char* input, Ast* ret) {
    if (input[0] == '(') {
        int c=0, i=1;
        for(; input[i] != ')' || c != 0; i++) {
            switch (input[i]) {
                case '(':
                    c++;
                    break;
                case ')':
                    c--;
                    break;
                case '\\':
                    i++;
                    break;
            }
        }

        input[i]=0;
        parse(&input[1], ret);
        input[i]=')';
        return i+1;
    }
    int offset = input[0]=='\\';
    *ret = create_lit(input[offset]);
    return offset + 1;
}

int parse(char* input, Ast* ret) {
    Ast end = create_seq(NULL, END);

    int len = 0, cap = 10, len2 = 0, cap2 = 10;
    Ast* seq = expect_nonnull(malloc(sizeof(Ast)*cap));

    Ast* ptr = expect_nonnull(malloc(sizeof(Ast)*cap2));
    seq[len++] = create_seq(ptr, CAT);

    int i=0;

    while (input[i]) {
        if (input[i] == '|') {
            seq[len-1].data.children[len2] = end;
            cap = ensure_nuf_space(&seq, len+2, cap, sizeof(Ast));
            i++;
            len2=0;
            cap2=10;
            Ast* ptr = expect_nonnull(malloc(sizeof(Ast)*cap2));
            seq[len++] = create_seq(ptr, CAT);
            continue;
        }
        
        Ast** currp = &seq[len-1].data.children;
        
        if (input[i] == '*') {
            cleanup((*currp)[--len2]);
            (*currp)[len2]=end;
            i++;
            continue;
        }

        cap2 = ensure_nuf_space(currp, len2+2, cap2, sizeof(Ast));

        Ast* curr = *currp;

        if (input[i] == '[') {
            curr[len2++]=create_seq(NULL, NONE);
            i+=2;
            continue;
        }

        i += parse_next(&input[i], &curr[len2++]);
    }

    seq[len-1].data.children[len2]=end;
    seq[len] = end;

    *ret = len == 1 ? seq[0] : create_seq(seq, OR);

    return i;
}

char* create_example(Ast ast) {
    switch (ast.tag) {
        case NONE:
        {
            return NULL;
        }
        case LIT:
        {
            char* a = expect_nonnull(malloc(sizeof(char)*2));
            a[0] = ast.data.ch;
            a[1] = 0;
            return a;
        }
        case OR:
        {
            Ast* children = ast.data.children;

            for (int i = 0; children[i].tag != END; i++) {
                char* segment = create_example(children[i]);
                if (segment != NULL) return segment;
            }

            return NULL;
        }
        case CAT:
        {
            int len = 0, cap = 10;
            char* res = expect_nonnull(malloc(sizeof(char)*cap));
            
            Ast* children = ast.data.children;

            for (int i = 0; children[i].tag != END; i++) {
                char* segment = create_example(children[i]);

                if (segment == NULL) {
                    free(res);
                    return NULL;
                }
                
                int l = strlen(segment);
                cap = ensure_nuf_space(&res, len+l+1, cap, sizeof(char));
                strcpy(&res[len], segment);
                len+=l;
            }

            res[len] = 0;
            return res;
        }
        case END: {}
    }
    return NULL;
}

int main(int argc, char *argv[]) {
    int cap=8, len=0;
    char* input=expect_nonnull(malloc(sizeof(char)*cap));

    while (1) {
        printf(">> ");
        fflush(stdout);

        char c;
        while ((c=getchar()) != '\n') {
            if (c==EOF) {
                free(input);
                return EXIT_SUCCESS;
            }
            cap = ensure_nuf_space(&input, len+2, cap, sizeof(char));
            input[len++]=c;
        }

        input[len]=0;

        Ast ast;

        parse(input, &ast);

        char* ex = create_example(ast);

        if (ex != NULL) {
            printf("%s\n", ex);
            free(ex);
        } else printf(":(\n");

        cleanup(ast);
        fflush(stdout);
    }
}