raz / test / bench / lines.pas

44 lines · 945 B · raw · history · last change c04ee33e

1{ Lines.raz's twin. }
2program lines;
3{$mode objfpc}
4const
5 N = 16777216;
6 Rounds = 32;
7 Letters = 'etaoinshrdlucmfw';
8var
9 text: array[0..N - 1] of byte;
10 i, r, x, v, k, total, walked: int64;
11
12function Count(p: pbyte; n: int64): int64;
13var i, c: int64;
14begin
15 c := 0;
16 for i := 0 to n - 1 do
17 if p[i] = 10 then inc(c);
18 Count := c;
19end;
20
21begin
22 x := 1;
23 for i := 0 to N - 1 do begin
24 x := (x * 1103515245 + 12345) and $7FFFFFFF;
25 v := (x shr 16) and 63;
26 if v = 0 then text[i] := 10
27 else if v < 10 then text[i] := ord(' ')
28 else text[i] := ord(Letters[(v and 15) + 1]);
29 end;
30 total := 0;
31 walked := 0;
32 for r := 1 to Rounds do begin
33 total := total + Count(@text[0], N);
34 i := 0;
35 k := IndexByte(text[0], N, 10);
36 while k >= 0 do begin
37 inc(walked);
38 i := i + k + 1;
39 if i >= N then break;
40 k := IndexByte(text[i], N - i, 10);
41 end;
42 end;
43 writeln(total, ' ', walked);
44end.