| 1 | (* The lines of a text, counted all at once with Bytes.count, and walked |
| 2 | one by one with Bytes.index: lines.pas does the same with FPC's |
| 3 | IndexByte, which is assembly, and a counting loop. *) |
| 4 | module Lines; |
| 5 | import Out, Bytes; |
| 6 | import SYSTEM; |
| 7 | check off; |
| 8 | |
| 9 | const |
| 10 | N = 16777216; |
| 11 | Rounds = 32; |
| 12 | Letters = "etaoinshrdlucmfw"; |
| 13 | |
| 14 | var text: [N]U8; |
| 15 | |
| 16 | proc main*; |
| 17 | var i, r, x, v, k, total, walked: INT; |
| 18 | begin |
| 19 | x := 1; |
| 20 | for i := 0 to N - 1 do |
| 21 | x := (x * 1103515245 + 12345) & 7FFFFFFFH; |
| 22 | v := (x shr 16) & 63; |
| 23 | if v = 0 then text[i] := 10; |
| 24 | elsif v < 10 then text[i] := U8(' '); |
| 25 | else text[i] := Letters[v & 15]; |
| 26 | end; |
| 27 | end; |
| 28 | total := 0; |
| 29 | walked := 0; |
| 30 | for r := 1 to Rounds do |
| 31 | total := total + Bytes.count(text, 10); |
| 32 | i := 0; |
| 33 | k := Bytes.index(text, 10); |
| 34 | while k >= 0 do |
| 35 | INC(walked); |
| 36 | i := i + k + 1; |
| 37 | k := Bytes.index(text[i..N], 10); |
| 38 | end; |
| 39 | end; |
| 40 | Out.int(total); |
| 41 | Out.str(" "); |
| 42 | Out.int(walked); |
| 43 | Out.ln; |
| 44 | end; |
| 45 | |
| 46 | end Lines. |