- 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
- 49 lines of Turing from the credited upstream file ccc06j3.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.
123456789 10var s : string11var t : int12 13loop14 get s15 exit when s = "halt"16 t := 017 for i : 1 .. length (s)18 if index("adgjmptw", s (i)) > 0 then19 t := t + 120 elsif index ("behknqux", s (i)) > 0 then21 t := t + 222 elsif index ("cfilorvy", s (i)) > 0 then23 t := t + 324 elsif index ("sz", s (i)) > 0 then25 t := t + 426 end if27 if i > 1 and ( (index ("abc", s (i - 1)) > 0 and28 index ("abc", s (i)) > 0) or29 (index ("def", s (i - 1)) > 0 and30 index ("def", s (i)) > 0) or31 (index ("ghi", s (i - 1)) > 0 and32 index ("ghi", s (i)) > 0) or33 (index ("jkl", s (i - 1)) > 0 and34 index ("jkl", s (i)) > 0) or35 (index ("mno", s (i - 1)) > 0 and36 index ("mno", s (i)) > 0) or37 (index ("pqrs", s (i - 1)) > 0 and38 index ("pqrs", s (i)) > 0) or39 (index ("tuv", s (i - 1)) > 0 and40 index ("tuv", s (i)) > 0) or41 (index ("wxyz", s (i - 1)) > 0 and42 index ("wxyz", s (i)) > 0)) then43 t := t + 244 end if45 end for46 put t47end loop48 49