omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / one_layer / generalization / dev / gen_expected.c
| 1 | /* |
| 2 | * gen_expected.c -- independent C computation of hidden/expected for the PUBLIC GEN dev tasks |
| 3 | * devgen01..devgen10 (aien-spark/omega#476; tests/one_layer/generalization/dev/). Written from each task's |
| 4 | * prompt, not from its reference.osc (closed forms or exact 128-bit arithmetic where the reference loops), |
| 5 | * so a reference that disagrees with its prompt is caught by `ola.sh tasks-check`. |
| 6 | * gen_expected <task_id> < hidden/inputs > hidden/expected |
| 7 | * Refuses (exit 1) an input outside what the prompt promises ("for every input checked ..."). |
| 8 | */ |
| 9 | #include <inttypes.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | typedef __int128 i128; |
| 15 | |
| 16 | static int rd(int64_t *v, int n) |
| 17 | { |
| 18 | char buf[256]; |
| 19 | if (!fgets(buf, sizeof buf, stdin)) return 0; |
| 20 | char *s = buf; |
| 21 | for (int i = 0; i < n; i++) { |
| 22 | char *e; |
| 23 | v[i] = strtoll(s, &e, 10); |
| 24 | if (e == s) { fprintf(stderr, "gen_expected: bad input line\n"); exit(1); } |
| 25 | s = e; |
| 26 | } |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | static void refuse(const char *t, const char *why) { fprintf(stderr, "gen_expected: %s: input %s\n", t, why); exit(1); } |
| 31 | |
| 32 | static void pr128(i128 v) |
| 33 | { |
| 34 | char b[64]; |
| 35 | int n = 0, neg = v < 0; |
| 36 | unsigned __int128 u = neg ? (unsigned __int128)(-(v + 1)) + 1 : (unsigned __int128)v; |
| 37 | do { b[n++] = (char)('0' + (int)(u % 10)); u /= 10; } while (u); |
| 38 | if (neg) putchar('-'); |
| 39 | while (n) putchar(b[--n]); |
| 40 | putchar('\n'); |
| 41 | } |
| 42 | |
| 43 | static int fits32(i128 v) { return v >= INT32_MIN && v <= INT32_MAX; } |
| 44 | static int fits64(i128 v) { return v >= INT64_MIN && v <= INT64_MAX; } |
| 45 | |
| 46 | int main(int argc, char **argv) |
| 47 | { |
| 48 | if (argc != 2) { fprintf(stderr, "usage: gen_expected <task_id> < inputs\n"); return 2; } |
| 49 | const char *t = argv[1]; |
| 50 | int64_t v[8]; |
| 51 | if (!strcmp(t, "devgen01")) while (rd(v, 1)) { |
| 52 | /* sum_{i<n} 3i^2 + 2i + 1 = (n-1)n(2n-1)/2 + (n-1)n + n */ |
| 53 | i128 n = v[0], s = (n - 1) * n * (2 * n - 1) / 2 + (n - 1) * n + n; |
| 54 | if (n == 0) s = 0; |
| 55 | if (s > UINT32_MAX) refuse(t, "result exceeds 32 bits"); |
| 56 | pr128(s); |
| 57 | } |
| 58 | else if (!strcmp(t, "devgen02")) while (rd(v, 2)) { |
| 59 | /* exact sum; i32 semantics: trap iff some partial result leaves i32. The prompt promises that |
| 60 | * either every partial result fits or the final sum does not fit; check that promise. */ |
| 61 | i128 n = v[0], c = v[1], s = 0; |
| 62 | int partial_overflow = 0; |
| 63 | for (i128 x = 0; x < n; x++) { |
| 64 | i128 a = x - c, b = x + c, p = a * b, g = p + x; |
| 65 | if (!fits32(a) || !fits32(b) || !fits32(p) || !fits32(g)) partial_overflow = 1; |
| 66 | s += g; |
| 67 | if (!fits32(s)) partial_overflow = 1; |
| 68 | } |
| 69 | if (partial_overflow && fits32(s)) refuse(t, "a partial result overflows but the final sum fits"); |
| 70 | if (partial_overflow) printf("TRAP\n"); else pr128(s); |
| 71 | } |
| 72 | else if (!strcmp(t, "devgen03")) while (rd(v, 2)) { |
| 73 | uint64_t s = (uint64_t)v[0]; |
| 74 | for (uint64_t i = 0; i < (uint64_t)v[1]; i++) s = (s * 31 + i * i + 7) % 65536; |
| 75 | printf("%" PRIu64 "\n", s); |
| 76 | } |
| 77 | else if (!strcmp(t, "devgen04")) while (rd(v, 1)) { |
| 78 | i128 n = v[0]; |
| 79 | pr128(n % 2 == 0 ? n * (n + 1) / 2 : n * n * n - 2 * n + 5); |
| 80 | } |
| 81 | else if (!strcmp(t, "devgen05")) while (rd(v, 2)) { |
| 82 | i128 x = v[0], k = v[1]; |
| 83 | if (x < 0) { pr128(3 * x * x + 5 * x - 2); continue; } |
| 84 | /* t_k = (x + 1) * 2^k - 1; 2t overflows before t_k does only if t_k overflows (2t <= 2^63 - 2) */ |
| 85 | i128 t1 = x + 1; |
| 86 | int over = 0; |
| 87 | for (i128 i = 0; i < k && !over; i++) { t1 *= 2; if (t1 - 1 > INT64_MAX) over = 1; } |
| 88 | if (over) printf("TRAP\n"); else { if (!fits64(t1 - 1)) refuse(t, "range"); pr128(t1 - 1); } |
| 89 | } |
| 90 | else if (!strcmp(t, "devgen06")) while (rd(v, 2)) { |
| 91 | i128 b = v[0], e = v[1]; |
| 92 | if (b < 10) { |
| 93 | unsigned __int128 r = 1; |
| 94 | for (i128 i = 0; i < e; i++) { r *= (unsigned __int128)b; if (r > UINT64_MAX) refuse(t, "b^e exceeds 64 bits"); } |
| 95 | pr128((i128)r); |
| 96 | } else pr128((b * b + e * e) * 3 + b); |
| 97 | } |
| 98 | else if (!strcmp(t, "devgen07")) while (rd(v, 4)) { |
| 99 | i128 best = (i128)v[1] - v[0]; |
| 100 | for (int i = 1; i < 3; i++) if ((i128)v[i + 1] - v[i] > best) best = (i128)v[i + 1] - v[i]; |
| 101 | pr128(best); |
| 102 | } |
| 103 | else if (!strcmp(t, "devgen08")) while (rd(v, 5)) { |
| 104 | int k = 0; |
| 105 | for (int i = 0; i < 4; i++) k += v[i + 1] > v[i]; |
| 106 | printf("%d\n", k); |
| 107 | } |
| 108 | else if (!strcmp(t, "devgen09")) while (rd(v, 2)) { |
| 109 | /* count by residues: pairs i < j with (i mod k) + (j mod k) = 0 mod k */ |
| 110 | int64_t n = v[0], k = v[1]; |
| 111 | if (k < 1) refuse(t, "k = 0"); |
| 112 | int64_t cnt[256] = {0}; |
| 113 | for (int64_t i = 1; i <= n; i++) cnt[i % k]++; |
| 114 | int64_t pairs = cnt[0] * (cnt[0] - 1) / 2; |
| 115 | for (int64_t r = 1; r < k; r++) { |
| 116 | int64_t q = k - r; |
| 117 | if (r < q) pairs += cnt[r] * cnt[q]; |
| 118 | else if (r == q) pairs += cnt[r] * (cnt[r] - 1) / 2; |
| 119 | } |
| 120 | printf("%" PRId64 "\n", pairs); |
| 121 | } |
| 122 | else if (!strcmp(t, "devgen10")) while (rd(v, 2)) { |
| 123 | /* for each i, the j count is min(n, m / i) */ |
| 124 | int64_t n = v[0], m = v[1], c = 0; |
| 125 | for (int64_t i = 1; i <= n; i++) { int64_t q = m / i; c += q < n ? q : n; } |
| 126 | printf("%" PRId64 "\n", c); |
| 127 | } |
| 128 | else { fprintf(stderr, "gen_expected: unknown task %s\n", t); return 2; } |
| 129 | return 0; |
| 130 | } |