- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 82 lines of Turing from the credited upstream file ccc99s1.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123456 78 910111213 1415 16var infile : string := "card.in"17var outfile : string := "card.out"18var fi, fo : int19var card : array 1 .. 52 of string20var a, b, v : int21 222324function noHigh (i : int, x : int) : boolean25 if i + x > 52 then26 result false27 end if28 for j : i + 1 .. i + x29 if card (j) = "ace"30 or card (j) = "king"31 or card (j) = "queen"32 or card (j) = "jack" then33 result false34 end if35 end for36 result true37end noHigh38 39open : fi, infile, get40open : fo, outfile, put41a := 042b := 043for i : 1 .. 5244 get : fi, card (i)45end for46 47for i : 1 .. 5248 if card (i) = "ace" and noHigh (i, 4) then49 v := 450 elsif card (i) = "king" and noHigh (i, 3) then51 v := 352 elsif card (i) = "queen" and noHigh (i, 2) then53 v := 254 elsif card (i) = "jack" and noHigh (i, 1) then55 v := 156 else57 v := 058 end if59 if v > 0 then60 var p : string61 if i mod 2 = 1 then62 p := "A"63 a := a + v64 else65 p := "B"66 b := b + v67 end if68 put : fo, "Player ", p, " scores ", v, " point(s)."69 end if70end for71 72put : fo, "Player A: ", a, " point(s)."73put : fo, "Player B: ", b, " point(s)."74 75close : fi76close : fo77 78 79 80 81 82