11 static int debug_level
= -1;
13 debug_raw(int level
, const char *fmt
, ...) {
16 debug_level
= atoi(getenv("DEBUG") ?: "0");
17 if (debug_level
>= level
) {
19 vfprintf(stderr
, fmt
, ap
);
23 #define debug(...) debug_raw(1, __VA_ARGS__)
25 static int __attribute__((noreturn
))
26 die(const char *fmt
, ...)
37 static char orig
[MAX
];
38 static char data
[2][MAX
* 10000];
44 printf("after phase %d, signal begins with %.8s\n", phase
, s
);
48 do_phase(char *old
, char *new, int offset
) {
50 static int8_t m
[] = { 0, 1, 0, -1 };
52 /* First half is painful */
53 for (i
= offset
; i
< len
/ 2; i
++) {
54 for (t
= 0, j
= offset
; j
< len
; j
++)
55 t
+= (old
[j
] - '0') * m
[(j
+ 1) / (i
+ 1) % 4];
56 new[i
] = (abs(t
) % 10) + '0';
57 debug(" i=%d: %c\n", i
, new[i
]);
59 /* Second half is easy */
60 for (t
= 0, i
= len
- 1; i
>= offset
&& i
>= len
/ 2; i
--) {
62 new[i
] = t
% 10 + '0';
63 debug(" i=%d: %c\n", i
, new[i
]);
71 main(int argc
, char **argv
) {
78 if (argc
> 1 && strcmp(argv
[1], "-"))
79 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
88 if (reps
> sizeof data
[0] / sizeof orig
)
91 while (isdigit(ch
= getchar())) {
94 die("recompile with larger MAX");
96 printf("Operating on length %d\n", len
);
100 memcpy(data
[0], orig
, len
);
101 for (i
= 0; i
< iter
; i
++)
102 do_phase(data
[phase
% 2], data
[1 - phase
% 2], 0);
103 dump(data
[phase
% 2]);
111 printf("ignoring offset %d\n", offset
);
114 if (offset
>= len
* 10000)
115 die("offset too large");
116 printf("need offset %d among len %d, try %d reps\n", offset
, len
* 10000,
117 (len
* 10000 - offset
+ (len
/ 2 - 1)) / (len
/ 2));
118 reps
= (len
* 10000 - offset
+ (len
/ 2 - 1)) / (len
/ 2);
119 offset
-= (10000 - reps
) * len
;
120 printf("trying offset %d among len %d instead\n", offset
, len
* reps
);
123 for (i
= 0; i
< reps
; i
++)
124 memcpy(&data
[0][i
* len
], orig
, len
);
127 for (i
= 0; i
< iter
; i
++) {
128 if (offset
< len
/ 2)
130 do_phase(data
[phase
% 2], data
[1 - phase
% 2], offset
);
134 printf("%.*s\n", len
, data
[phase
% 2]);
135 dump(&data
[phase
% 2][offset
]);